Except where otherwise noted, the contents of this document are Copyright 2012 Marty Stepp, Jessica Miller, Victoria Kirst and Roy McElmurry IV. All rights reserved. Any redistribution, reproduction, transmission, or storage of part or all of the contents in any form is prohibited without the author's expressed written permission.
display
property
h2 { display: inline; background-color: yellow; }
property | description |
---|---|
display
|
sets the type of CSS box model an element is displayed with |
none
, inline
, block
, run-in
, compact
, ...<ul id="topmenu"> <li>Item 1</li> <li>Item 2</li> <li>Item 3</li> </ul>
#topmenu li { display: inline; border: 2px solid gray; margin-right: 1em; }
We will develop on this beer blog page.
visibility
propertyp.secret { visibility: hidden; }
Since nobody can see this anyway: ca-ca poo-poo pee-pee!!!
property | description |
---|---|
visibility
|
sets whether an element should be shown onscreen; can be visible (default) or hidden
|
hidden
elements will still take up space onscreen, but will not be shown
display
to none
insteadopacity
property
body { background-image: url("images/marty-mcfly.jpg"); background-repeat: repeat; } p { background-color: yellow; margin: 0; padding: 0.25em; } p.mcfly1 { opacity: 0.75; } p.mcfly2 { opacity: 0.50; } p.mcfly3 { opacity: 0.25; }
Marty McFly in 1985
Marty McFly in 1955 fading away, stage 1
Marty McFly in 1955 fading away, stage 2
Marty McFly in 1955 fading away, stage 3
property | description |
---|---|
opacity
|
how not-transparent the element is; value ranges from 1.0 (opaque) to 0.0 (transparent) |
http://server/path/file
https://webster.cs.washington.edu/cse190m/quote.php
webster.cs.washington.edu
to run the program quote2.php
and send back its output.html
file (static content): server just sends that file.php
file (dynamic content): server reads it, runs any script code inside it, then sends result across the network
There are many other options for server-side languages: Ruby on Rails, JSP, ASP.NET, etc. Why choose PHP?
php.net/functionName
in browser Address bar to get docs for any function
The following contents could go into a file hello.php
:
<?php print "Hello, world!"; ?>
<?php
and ends with ?>
.php
page on your local hard drive; you'll either see nothing or see the PHP source code.php
file will run the program and send you back its outputprint
print "text";
print "Hello, World!\n"; print "Escape \"chars\" are the SAME as in Java!\n"; print "You can have line breaks in a string."; print 'A string can use "single-quotes". It\'s cool!';
echo
instead of print
+ - * / %
. ++ --
= += -= *= /= %= .=
5 + "7"
is 12
$name = expression;
$user_name = "PinkHeartLuvr78"; $age = 16; $drinking_age = $age + 5; $this_class_rocks = TRUE;
$
, on both declaration and usage# single-line comment // single-line comment /* multi-line comment */
#
is also allowed
#
comments instead of //
#
and will use it in our examplesfor
loop
for (initialization; condition; update) { statements; }
for ($i = 0; $i < 10; $i++) { print "$i squared is " . $i * $i . ".\n"; }
We will work our way toward the ideal solution to this problem as we learn more and more about PHP.
if/else
statementif (condition) { statements; } elseif (condition) { statements; } else { statements; }
elseif
keyword is much more common, else if
is also supported$a = 3; $b = 4; $c = sqrt(pow($a, 2) + pow($b, 2));
abs
|
ceil
|
cos
|
floor
|
log
|
log10
|
max
|
min
|
pow
|
rand
|
round
|
sin
|
sqrt
|
tan
|
M_PI
|
M_E
|
M_LN2
|
int
and float
types$a = 7 / 2; # float: 3.5 $b = (int) $a; # int: 3 $c = round($a); # float: 4.0 $d = "123"; # string: "123" $e = (int) $d; # int: 123
int
for integers and float
for realsint
values can produce a float
String
type
$favorite_food = "Ethiopian";
print $favorite_food[2]; # h
.
(period), not +
5 + "2 turtle doves"
produces 7
5 . "2 turtle doves"
produces "52 turtle doves"
""
or ''
$age = 16; print "You are " . $age . " years old.\n"; print "You are $age years old.\n"; # You are 16 years old.
" "
are interpreted
' '
are not interpreted:
print 'You are $age years old.\n'; # You are $age years old.\n
{}
:
print "Today is your $ageth birthday.\n"; # $ageth not found print "Today is your {$age}th birthday.\n";
String
functions# index 0123456789012345 $name = "Stefanie Hatcher"; $length = strlen($name); # 16 $cmp = strcmp($name, "Brian Le"); # > 0 $index = strpos($name, "e"); # 2 $first = substr($name, 9, 5); # "Hatch" $name = strtoupper($name); # "STEFANIE HATCHER"
Name | Java Equivalent |
---|---|
strlen |
length |
strpos |
indexOf |
substr |
substring |
strtolower , strtoupper |
toLowerCase , toUpperCase |
trim |
trim |
explode , implode |
split , join |
strcmp |
compareTo |
$feels_like_summer = FALSE;
$php_is_rad = TRUE;
$student_count = 217;
$nonzero = (bool) $student_count; # TRUE
FALSE
(all others are TRUE
):
0
and 0.0
""
, "0"
, and NULL
(includes unset variables)(bool)
FALSE
prints as an empty string (no output); TRUE
prints as a 1
TRUE
and FALSE
keywords are case insensitiveHTML content <?php PHP code ?> HTML content <?php PHP code ?> HTML content ...
.php
file between <?php
and ?>
are executed as PHP codeWe will work our way toward the ideal solution to this problem as we learn more and more about PHP.
<?php print "<!DOCTYPE html>\n"; print "<html>\n"; print " <head>\n"; print " <title>Geneva's web page</title>\n"; ... for ($i = 1; $i <= 10; $i++) { print "<p class=\"count\"> I can count to $i! </p>\n"; } ?>
print
statements is bad style and error-prone:
\"
print
, how do we insert dynamic content into the page?We will work our way toward the ideal solution to this problem as we learn more and more about PHP.
<?= expression ?>
<h2> The answer is <?= 6 * 7 ?> </h2>
<?= expr ?>
is equivalent to <?php print expr; ?>
<body> <p>Watch how high I can count: <?php for ($i = 1; $i <= 10; $i++) { ?> <? $i ?> </p> </body> </html>
</body>
and </html>
above are inside the for
loop, which is never closed$end
'=
in <?=
, the expression does not produce any output<body> <?php for ($i = 1; $i <= 3; $i++) { ?> <h<?= $i ?>>This is a level <?= $i ?> heading.</h<?= $i ?>> <?php } ?> </body>
<!DOCTYPE html> <html> <head><title>CSE 190 M: Embedded PHP</title></head> <body> <?php for ($i = 99; $i >= 1; $i--) { ?> <p> <?= $i ?> bottles of beer on the wall, <br /> <?= $i ?> bottles of beer. <br /> Take one down, pass it around, <br /> <?= $i - 1 ?> bottles of beer on the wall. </p> <?php } ?> </body> </html>