My PHP String
"; //We can use a variable inside “ ”
$nf2 = number_format(12345, 2); // 12,345.00
echo " $nf2
"; //We can use a variable inside “ ”
$nf3 = number_format(12345.674, 2); // 12,345.67
echo " $nf3
"; //We can use a variable inside “ ”
$nf4 = number_format(12345.675, 2); // 12,345.68
?>
Go to Next Page
My PHP page";
echo "Today is " . date("Y/m/d") . "
";
echo "Today is " . date("Y.m.d") . "
";
echo "Today is " . date("Y-m-d") . "
";
echo "Today is " . date("l")."
";
echo "The time is " . date("h:i:sa") ."
";
date_default_timezone_set("America/New_York");
echo "The time is " . date("h:i:sa") . "
";
echo "© 2010-" . date("Y") . "
";
?>
Go to Next Page
My PHP page";
$h = date("H");
echo "The hour (of the server) is " . $h;
if ($h < "20") {
echo " Have a good day!";
}
$m = date("M");
echo "
The month (of the server) is " . $m;
if ($m == "Apr") {
echo " This is April!";
} else {
echo " This is NOT April!";
}
$y = date("Y");
echo "
The year (of the server) is " . $y;
if ($y < "2015") {
echo " Old years!";
} elseif ($y < "2016") {
echo " Last Year!";
} else {
echo " This year!";
}
?>
Go to Next Page
My PHP page";
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break; //without break, next case will be executed.
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
Go to Next Page
My PHP page";
//while loop first sets a variable $x to 1.
$x = 1;
while($x <= 5) {
//Then, the while loop will continue to run as long as $x is less than, or equal to 5 ($x <= 5).
echo "The number is: $x
";
$x++; // $x will increase by 1 each time the loop runs ($x++):
}
echo "
";
//do while loop first sets a variable $y to 1.
$y = 1;
do { //the do while loop will write some output,
echo "The number is: $y
";
$y++; //then increment the variable $y with 1.
} while ($y <= 5);
//the condition is checked (is $y less than, or equal to 5?),
//and the loop will continue to run as long as $y is less than, or equal to 5:
echo "
";
// for loop This will display the numbers from 0 to 10:
for ($i = 0; $i <= 10; $i++) {
echo "The number is: $i
";
}
echo "
";
// foreach The following example demonstrates a loop that will output the values of the given array ($colors):
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value
";
}
?>
Go to Next Page
My PHP page";
//In the example below, we create a function named "writeMsg()".
function writeMsg() {
echo "Hello world!";
}
//To let a function return a value, use the return statement:
function sum($x, $y) {
$z = $x + $y;
return $z;
}
//function calls
writeMsg(); //Calling the writeMsg() function
echo "
";
$result = sum(5, 10) . "
"; //Calling the sum($x, $y) function with two parameters (5, 10)
//Then there is a return value to $result. Then it will change line for
echo "5 + 10 = " . $result . "
";
echo "7 + 13 = " . sum(7, 13) . "
"; //Calling the sum($x, $y) function with two parameters(7, 13)
?>
Go to Next Page
My PHP page";
//To loop through and print all the values of an indexed array,
//you could use a for loop, like this:
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; //index starts from 0
//The count() function is used to return the length (the number of elements) of an array:
$arrlength = count($cars); //3 elements
//This will print all elements from $cars
echo "
" . "The length of cars is " . $arrlength;
echo "
" . "Printing Original cars array using index number:" . "
";
for ($x = 0; $x < $arrlength; $x++) {
echo $cars[$x] . "
";
}
//This will sorts the elements of the $cars array in ascending alphabetical order:
sort($cars);
echo "
After sort, printing cars array with foreach loop :" . "
"; //you can put
this way.
//This will print all elements from $cars
foreach ($cars as $value) {
echo "$