Please send questions to
st10@humboldt.edu .
* PHP is running on redwood --- put your .php file in your public_html
directory on redwood, and then, for user abc23 with php file
myfile.php, the URL would be:
http://redwood.humboldt.edu/~abc23/myfile.php
* more PHP language basics:
* (oops --- PHP statements DO terminate with a semicolon,
but the closing tag ?> sticks one in...!)
* addendum to PHP variable basics:
* after the $, needs to start with letter or underscore
... followed by any number of letters, digits, or underscores
* PHP IS case sensitive!
* PHP types...
* the type of a variable is NOT set by the programmer ---
instead, it is decided at runtime BY PHP, depending on
the CONTEXT in which that variable is used;
* supports 8 primitive types:
* 4 scalar types...
* boolean (2 literals: TRUE FALSE)
* integer
* float
* string
* 2 compound types:
* array
* object
* 2 special types:
* resource
* NULL
* if you'd like a human-friendly printout of the type of a
variable, gettype function provides that...
print gettype($a_variable);
* BUT for seeing if something is of a particular type,
better to use the is_<type> functions...
is_int($var)
is_string($var)
* see: type_play.php
* operators
* . for string concatenation (like Perl, unlike Java)
* arithmetic operators: pretty much like you'd guess!
+ - * /
* operator precedence: also mostly what you'd expect ---
see
http://www.php.net/manual/en/language.operators.php#language.operators.precedence
* = is assignment
* ++ -- are there, too
* comparisons evaluate to TRUE or FALSE (starting in PHP 4)
> >= < <=
== does compare for equality
!= does compare for inequality
BUT: === equal to and same type
!== not equal to OR not same type
* += *= -= /=
* how about logical/boolean operators?
* and or
&& ||
...both! BUT && and || have higher operator precedence!
and ! for not...
* what about the branching structure?
* PHP has an if that's VERY similar to C++/Java
if (expr)
{
sttmt;
sttmt;
}
* yes, you can have an else:
if (expr)
{
sttmt;
sttmt;
}
else
{
sttmt;
sttmt;
}
* SLIGHT twist: there is also an optional elseif
if (expr)
{
sttmt;
sttmt;
}
elseif (expr)
{
sttmt;
sttmt;
}
else
{
sttmt;
sttmt;
}
* see if_and_equality.php
* what about repetition?
* there are while and for loops that should look very familiar!
(and other loops, too)
* while
while (expr)
{
sttmt;
sttmt;
}
* for
for (<init>; <cond>; <update>)
{
sttmt;
sttmt;
}
for ($i = 0; $i < $something; $i++)
{
sttmt;
sttmt;
}
* see loops1.php
* and, how about the function/procedure structure?
* many functions ARE built-in --- see: http://www.php.net/quickref.php
* but, you can write your own, also...
function <function_name> ( <parameters_sep'd_by_commas> )
{
// function code
return <expr>; // if desired
return; // if desired
}
* (return DOES have you leave the function....)
* see try_funct1.php, hello_funct.php
* and note that you can break "out" of PHP to insert HTML,
pretty much anywhere --- see if_and_more.php
* what if code is in another file?
include() statement includes and evaluates the specified file
* see vars.php, include1.php
* so, let's say a few words about arrays...
* really an ordered map, in PHP...
* (maps values to keys...)
* one way to set up an array in PHP:
array() language construct
$shopping = array("fruit" => "apple", 13 => 9,
"cereal" => "Grape Nuts");
* then, to grab the element corresponding to a key....
print $shopping["fruit"]
print $shopping[13]
print $shopping["cereal"]
* what if you don't GIVE a key for a value?
* then it'll find the maximum integer index included,
and a key that's that max + 1 will be assigned to such
a value.
* what if there ARE no integer indices at the time?!
... then it starts at index 0
* foreach loop to walk through array:
foreach ($arr as $value)
* $arr is the array to iterate through
* $value is SET to each value in the array $arr in turn
foreach ($arr as $key => $value)
* $arr is the array to iterate through
* $key is SET to each key in the array in turn,
and $value is set to that $key's value in turn
* array1.php plays with this, now, too!
* and to assign a new key-value pair within the array?
$my_arr[<new_key>] = <value>
(note that assigning to a key REPLACES its former value, if there)
* this is in array1.php, too
* in PHP, keys are limited to strings and integers;
...but VALUES can be ANY PHP type