Category Archives: PHP

PHP Tutorial

PHP Function

PHP Function Function syntax function function_name() { php code; } Function example <html> <body> <?php function Test() { echo “I like PHP functions!”; } Test(); ?> </body> </html> Result I like PHP functions! Function with parameters <html> <body> <?php function pFunction($p_name, $p_price) { echo “The $p_name price is: $p_price <br />”; } pFunction(“PHP”, “23”); pFunction(“HTML”, “18”); ?> </body>… Read More »

PHP Arrays

PHP Arrays An array in PHP is a type that associates values to keys. This type can be an array, list, hash table, dictionary, collection, stack, queue. Simple array example <html> <body> <?php $courses = array(“PHP”, “MySQL”); echo “I learn ” . $courses[0] . ” and ” . $courses[1] . “.”; echo “<br />”; echo count($courses), ” courses.”;… Read More »

PHP For

PHP For For syntax for (initialization; condition; incrementation) { execute statement } For example <html> <body> <?php for ($n = 1; $n <= 5; $n++) { echo “<br />”; echo “n = “, $n; } ?> </body> </html> Result n = 1 n = 2 n = 3 n = 4 n = 5

PHP Do…While

PHP Do…While Do…While syntax do{ execute statement }while (expression is true) ; Do…While example <html> <body> <?php $n = 1; do{ echo “<br />”; echo “n = “, $n++; } while ($n <= 3) ; ?> </body> </html> Result n = 1 n = 2 n = 3

PHP While

PHP While While syntax while (expression is true) { execute statement } While example <html> <body> <?php $x = 1; while ($x <= 3) { echo “<br />”; echo “x = “, $x++; } ?> </body> </html> Result x = 1 x = 2 x = 3

PHP Switch

PHP Switch Switch syntax switch (x) { case x1: if x=x1 then execute statement break; case x2: if x=x2 then execute statement break; case x3: if x=x3 then execute statement break; … default: statement to be executed if x is not equal to x1, x2 or x3; } Switch example <html> <body> <?php $x = 1; switch ($x)… Read More »

PHP IF – Else – Elseif

PHP IF – Else – Elseif Syntax if (condition) { if condition is true then execute statement } if (condition) { if condition is true then execute statement } else { if condition is false then execute statement } if (A) { if A is true then execute statement } elseif (B) { if A is false and… Read More »

PHP Strings

PHP Strings Strings example <html> <body> <?php echo strlen(“PHP beginner course!”); echo “<br />”; echo str_word_count(“Learn about PHP strings.”); echo “<br />”; echo strtolower(“PHP online course!”); echo “<br />”; echo str_replace(“beginner”, “advanced”, “PHP beginner!”); ?> </body> </html> Result 20 4 php online course! PHP advanced!

PHP Constants

PHP Constants A constant is case-sensitive by default. By convention, constant identifiers are always uppercase. Constants syntax define(name, value, case-insensitive) or define(name, value) Constants example <html> <body> <?php define(“C1”, “PHP beginner course<br />”, true); echo c1; define(“C2”, “PHP advanced course<br />”, false); echo C2; define(“C3”, “PHP online<br />”); echo C3; define(“C4”, “Learn PHP Constants”); function functionTest() { echo… Read More »

PHP Operators

PHP Operators The Arithmatic Operators: Example Name Result $n + $z Addition Sum of $n and $z. $n – $z Subtraction Difference of $n and $z. $n * $z Multiplication Product of $n and $z. $n / $z Division Quotient of $n and $z. $n % $z Modulus Remainder of $n divided by $z. $n ** $z Exponentiation… Read More »