Monday, 21 November 2011

php lecture 8


this is lecture8 NT322 Lecture #9 # Wednesday November 8 / Friday November 11, 2011 # Basic PHP Language Syntax: // while loop: 12345678910
0123456789
12345678910
Current value of $v: 1
Current value of $v: 2
Current value of $v: 3
Current value of $v: 17

$a[0] is 2.3
0$a[1] is 98
1$a[2] is Hello World
2$a[3] is Seneca College
3$a[4] is 3.14
4$a[5] is 451
5
after math operations... $a[0] is 6.67
after math operations... $a[1] is 1862
after math operations... $a[2] is 0
after math operations... $a[3] is 0
after math operations... $a[4] is 9.8596
after math operations... $a[5] is 203401

i equals 2
$i is: 2
SERVER_NAME is: zenit.senecac.on.ca
PHP_SELF is: /lecture.php
REMOTE_ADDR is: 70.25.225.30
i is: 99
in the function $x is: YYY
outside of the function...$x is: ABC
Monday November 21 2011 EST10:39:22 PMportion is: 'sea shells by the seashore' string1 is: 'she sells sea shells by the seashore' strrev and strtoupper: 'EROHSAES EHT YB SLLEHS AES SLLES EHS'




test



this is lecture8


NT322 Lecture #9
# Wednesday November 8 / Friday November 11, 2011
# Basic PHP Language Syntax:

// while loop:
\n";

// do/while loop:
$i = 0;
do {
print $i++;
} while ($i< 10); echo "
\n";

// for loop:
for($i = 1; $i <= 10; $i++) { print $i; } echo "
\n";

// foreach loops:
$a = array(1, 2, 3, 17); // declaring arrays

foreach($a as $v) {
print "Current value of \$v: $v
";
}
echo "
\n";

/* values and keys */
$a = array(2.3, 98, "Hello World", "Seneca College", 3.14, 451);
$b = array(2.9, 19, "Hello World", "Seneca College", 3.14, 451);

foreach($a as $k => $v) {
print "\$a[$k] is $v
\n";

}
echo "
\n";

foreach($a as $k => $v) {
$a[$k] *= $b[$k];
print "after math operations... \$a[$k] is $a[$k]
\n";
}
echo "
\n";

$i = 2;

// switch/case:
switch ($i) {
case 0:
print "i equals 0";
break;
case 1:
print "i equals 1";
break;
case 2:
print "i equals 2";
break;
default:
print "i is not equal to 0, 1 or 2";
}
echo "
\n";

// control structures:
if($i == 0) {
print "\$i is: $i
\n";
}
else if($i == 1) {
print "\$i is: $i
\n";
}
else if($i == 2) {
print "\$i is: $i
\n";
}
else if($i == 3) {
print "\$i is: $i
\n";
}
else {
print "\$i is > 3: $i
\n";
}

// predefined variables:

$x = $_SERVER['SERVER_NAME'];
print "SERVER_NAME is: $x
\n";
// the name of the server host

$x = $_SERVER['REQUEST_METHOD'];
// 'GET', 'HEAD', 'POST' or 'PUT'

$x = $_SERVER['PHP_SELF'];
print "PHP_SELF is: $x
\n";
// the filename of the currently executing script

$x = $_SERVER['REMOTE_ADDR'];
print "REMOTE_ADDR is: $x
\n";
// the IP address from which the user is viewing the current page.

$i = 99;
?>

\n";

function display($myparam){
$x = $myparam;
print "in the function \$x is: $x
\n";
}
display("YYY");
print "outside of the function...\$x is: $x
\n";
// will output: "ABC"

// DATE functions:

echo date("l F d Y T");

// date( ) built-in PHP function.
// l = day_of_week name
// F = month name
// d = 2-digit day of month
// Y = 4 digit year
// T = timezone

echo date("h:i:s A");

// h = 2-digit hours
// i = 2-digit minutes
// s = 2-digit seconds
// A = AM/PM

// common variable functions
/*
empty - Determine whether a variable is set
isset - same as !empty($a)
gettype - Get the type of a variable
get_defined_vars - Returns an array of all defined variables
is_array - Finds whether a variable is an array
is_bool - Finds out whether a variable is a boolean
is_float - Finds whether a variable is a float
is_int - Find whether a variable is an integer
is_null - Finds whether a variable is NULL
is_numeric - Finds whether a variable is a number or a numeric string
is_object - Finds whether a variable is an object
is_string - Finds whether a variable is a string
unset - Unset a given variable
functions for debugging :
print_r - Prints human-readable information about a variable
var_dump - Dumps information about a variable
*/

// common string functions:
/*
chop - Strip whitespace from the end of a string
chr - Return a specific character
crypt - DES-encrypt a string
htmlspecialchars - Convert special characters to HTML entities
md5 - Calculate the md5 hash of a string
nl2br - Inserts HTML line breaks before all newlines in a string
ord - Return ASCII value of character
sprintf - Return a formatted string
strip_tags - Strip HTML and PHP tags from a string
addslashes - Quote string with slashes
stripslashes - Un-quote string quoted with addslashes()
strlen - Get string length
strpos - Find position of first occurrence of a string
strrev - Reverse a string
strtolower - Make a string lowercase
strtoupper - Make a string uppercase
str_replace - Replace all occurrences of the search string with the replacement string
join - Join array elements with a string
split - split string into array by regular expression
*/

$string1 = "she sells sea shells by the seashore";
$portion = substr($string1, 10);
echo "portion is: '$portion'
\n";
echo "string1 is: '$string1'
\n";
echo "strrev and strtoupper: '" . strrev(strtoupper($string1)) . "'
\n";

// common array functions:
/*
count - Count elements in an array/object
array_pop - Pop the element off the end of array
array_push - Push one or more elements onto the end of array
array_reverse - Return an array with elements in reverse order
array_shift - Shift an element off the beginning of array
array_unshift - Prepend one or more elements to the beginning of array
array_sum - Calculate the sum of values in an array.
array_unique - Removes duplicate values from an array
array_values - Return all the values of an array
in_array - Return TRUE if a value exists in an array
array_search - Searches the array for a given value and returns the corresponding key if successful
sizeof - Get the number of elements in an array/object
sort - Sort an array
uksort - Sort an array by keys using a user-defined comparison function
usort - Sort an array by values using a user-defined comparison function
*/
?>



foreach syntaxes

foreach

The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable. There are two syntaxes:
foreach (array_expression as $value)
    statement
foreach (array_expression as $key => $value)
    statement
The first form loops over the array given by array_expression. On each iteration, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next iteration, you'll be looking at the next element).
The second form will additionally assign the current element's key to the $key variable on each iteration.
It is possible to customize object iteration.
Note:
When foreach first starts executing, the internal array pointer is automatically reset to the first element of the array. This means that you do not need to call reset() before a foreach loop.
As foreach relies on the internal array pointer changing it within the loop may lead to unexpected behavior.
In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.
<?php
$arr 
= array(1234);
foreach (
$arr as &$value) {
    
$value $value 2;
}
// $arr is now array(2, 4, 6, 8)unset($value); // break the reference with the last element?>
Referencing $value is only possible if the iterated array can be referenced (i.e. if it is a variable). The following code won't work:
<?phpforeach (array(1234) as &$value) {
    
$value $value 2;
}
?>
 
Warning
Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().
Note:
foreach does not support the ability to suppress error messages using '@'.
You may have noticed that the following are functionally identical:
<?php
$arr 
= array("one""two""three");reset($arr);
while (list(, 
$value) = each($arr)) {
    echo 
"Value: $value<br />\n";
}

foreach (
$arr as $value) {
    echo 
"Value: $value<br />\n";
}
?>

 
The following are also functionally identical:
<?php
$arr 
= array("one""two""three");reset($arr);
while (list(
$key$value) = each($arr)) {
    echo 
"Key: $key; Value: $value<br />\n";
}

foreach (
$arr as $key => $value) {
    echo 
"Key: $key; Value: $value<br />\n";
}
?>
 
Some more examples to demonstrate usages:
<?php/* foreach example 1: value only */
$a = array(12317);

foreach (
$a as $v) {
    echo 
"Current value of \$a: $v.\n";
}
/* foreach example 2: value (with its manual access notation printed for illustration) */
$a = array(12317);
$i 0/* for illustrative purposes only */
foreach ($a as $v) {
    echo 
"\$a[$i] => $v.\n";
    
$i++;
}
/* foreach example 3: key and value */
$a = array(
    
"one" => 1,
    
"two" => 2,
    
"three" => 3,
    
"seventeen" => 17);

foreach (
$a as $k => $v) {
    echo 
"\$a[$k] => $v.\n";
}
/* foreach example 4: multi-dimensional arrays */$a = array();$a[0][0] = "a";$a[0][1] = "b";$a[1][0] = "y";$a[1][1] = "z";

foreach (
$a as $v1) {
    foreach (
$v1 as $v2) {
        echo 
"$v2\n";
    }
}
/* foreach example 5: dynamic arrays */
foreach (array(12345) as $v) {
    echo 
"$v\n";
}
?>