PHP

PHP Syntax

OutPut
Digital Learining Goal
Digital Learining Goal
Digital Learining Goal

<!DOCTYPE html>
<html>
<body>

<?php
ECHO “Digital Learining Goal<br>”;
echo “Digital Learining Goal<br>”;
EcHo “Digital Learining Goal<br>”;
?>

</body>
</html>

Comments 

OutPut
Hello World!
Hello World!
Hello World!

<!DOCTYPE html>
<html>
<body>

<?php
// This is a single-line comment

# This is also a single-line comment

/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
?>

</body>
</html>

Variables

OutPut
DLG!
7
14

<!DOCTYPE html>
<html>
<body>

<?php
$nou1= “DLG!”;
$num1 = 7;
$num2 = 14;

echo $nou1;
echo “<br>”;
echo $num1;
echo “<br>”;
echo $num2;
?>

</body>
</html>

Variables Scope

OutPut

Variable num1 inside =

Variable num1 outside = 7

<?php
$num1 = 7; // global scope

function myDLG() {
//if using num1 inside this function will be error
echo “<p>Variable num1 inside = $num1 </p>”;
}
myDLG();

echo “<p>Variable num1 outside = $num1 </p>”;
?>

OutPut

Variable num1 inside = 7

Variable num1 outside=

<?php
function myDLG1() {
$num1= 7; // local scope
echo “<p>Variable num1 inside = $num1</p>”;
}
myDLG1();

// using num1 outside the function will be error
echo “<p>Variable num1 outside= $num1</p>”;
?>

OutPut
7
8
9

<?php
function myDLG2() {
static $num1 = 7;
echo $num1;
$num1++;
}
myDLG2();
echo “<br>”;
myDLG2();
echo “<br>”;
myDLG2();
?>

echo / print

OutPut

DLG

Name : الهدف للتعليم الرقمي
14Digital Learining Goal!

<?php
$text1 = “DLG”;
$text2 = “الهدف للتعليم الرقمي “;
$num1 = 7;
$num2 = 7;

echo “<h2>” . $text1 . “</h2>”;
echo “Name : ” . $text2 . “<br>”;
echo $num1 + $num2 ;
print “Digital Learining Goal!<br>”;

?>

Data Types

OutPut
Digital Learining Goal

<!DOCTYPE html>
<html>
<body>

<?php
$x = “Digital Learining Goal”;
echo $x;
echo “<br>”;

?>

</body>
</html>

Note:String / Integer /Float / Boolean /Array /Object / NULL / Resource

string 

OutPut

<!DOCTYPE html>
<html>
<body>

<?php
// use strlen returns the length
echo strlen(“Hello world!”)”<br>”;

/*use str_word_count counts the number of words */
echo str_word_count(“Hello world!”);
“<br>”;

//use strrev reverses a string.
echo strrev(“Hello world!”);
“<br>”;

/*use strpos searches for a specific text If a match is found returns the character position If no match is found, return FALSE */
echo strpos(“Hello world!”, “world”);
“<br>”;

?>

</body>
</html>

Numbers

OutPut
bool(true)
bool(false)
bool(true)
float(INF)

<?php
// Check the type is integer
$num1= 14;
var_dump(is_int($num1));

echo “<br>”;
$num2= 14.7;
var_dump(is_int($num2));
echo “<br>”;
// Check the type is float
$num3= 12.56;
var_dump(is_float($num3));
echo “<br>”;
// Check value is finite or infinite
$num4=1.9e411;
var_dump($num4);
?>

Math

OutPut
pi() Function=
3.1415926535898
min() and max() Functions =
-10
15

<?php
echo “pi() Function=<br>”;
echo(pi()) ;
echo “<br>”;

echo “min() and max() Functions
=<br>”;
echo(min(7, 8, 15, -10) . “<br>”);
echo(max(7, 8, 15, -10));
?>

if…else…elseif Statements

OutPut
Digital Learining Goal

<?php
$text1 = date(“H”);

if ($text1 < “17”) {
echo “Digital Learining Goal”;
} else {
echo “DLG”;
}
?>

OutPut
Digital Learining Goal

<?php
$text1= date(“H”);
if ($text1< “7”) {
echo “Digital Learining Goal”;
} elseif ($text1< “15”) {
echo “DLG”;
} else {
echo “الهدف للتعليم الرقمي “;
}
?>

switch 

OutPut
The letters is : B

<?php
$letters = “B”;

switch ($letters ) {
case “A”:
echo “The letters is : A”;
break;
case “B”:
echo “The letters is : B”;
break;
case “C”:
echo “The letters is : C”;
break;
default:
echo “Not Found”;
}
?>

while Loop

OutPut
The number is: 0
The number is: 2
The number is: 4
The number is: 6
The number is: 8
The number is: 10
The number is: 12

<?php
$num = 0;
while($num<= 14) {
echo “The number is: $num<br>”;
$num+=2;
}
?>

 do…while Loop

OutPut
The number is: 0
The number is: 1
The number is: 2
The number is: 3
The number is: 4

<?php
$num = 0;
do {
echo “The number is: $num <br>”;
$num ++;
} while ($num <= 4);
?>

for-Loop

OutPut
The number is: 0
The number is: 2
The number is: 4
The number is: 6
The number is: 8
The number is: 10

<?php
for ($i = 0; $i <= 10; $i+=2) {
echo “The number is: $i <br>”;
}
?>

foreach-Loop

OutPut
txt1= 152
tet2= 258754
txt3= 25452

<?php
$ID = array(“txt1″=>”152”, “tet2″=>”258754”, “txt3″=>”25452”);

foreach($ID as $i=> $val) {
echo “$i= $val<br>”;
}
?>

 Function Arguments

OutPut
The height is : 350
The height is : 50
The height is : 135
The height is : 80

<!DOCTYPE html>
<html>
<body>

<?php
function setHeight(int $minheight = 50) {
echo “The height is : $minheight <br>”;
}

setHeight(350);
setHeight();
setHeight(135);
setHeight(80);
?>

</body>
</html>

Return-Type-Declarations

OutPut
6.4

<?php
function addNumbers(float $a, float $b) : float {
return $a + $b;
}
echo addNumbers(1.2, 5.2);
?>

Arrays

OutPut
3

<!DOCTYPE html>
<html>
<body>

<?php
$carsAr1 = array(“Volvo”, “BMW”, “Toyota”);
echo count($carsAr1);
?>

</body>
</html>

Multidimensional-Arrays

OutPut
Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43

<!DOCTYPE html>
<html>
<body>

<?php
$ageAr2 = array(“Peter”=>”35”, “Ben”=>”37”, “Joe”=>”43”);

foreach($ageAr2 as $x => $x_value) {
echo “Key=” . $x . “, Value=” . $x_value;
echo “<br>”;
}
?>

</body>
</html>

Sorting-Arrays

OutPut
2
4
6
11
22

<!DOCTYPE html>
<html>
<body>

<?php
$numbersAr3 = array(4, 6, 2, 22, 11);
sort($numbersAr3);

$arrlengthAr3 = count($numbersAr3);
for($x = 0; $x < $arrlengthAr3; $x++) {
echo $numbersAr3[$x];
echo “<br>”;
}
?>

</body>
</html>

match-all

OutPut
4

<!DOCTYPE html>
<html>
<body>

<?php
$strr1 = “The rain in SPAIN falls mainly on the plains.”;
$patternr1 = “/ain/i”;
echo preg_match_all($patternr1, $strr1);
?>

</body>
</html>

replace

OutPut
Visit W3Schools!

<!DOCTYPE html>
<html>
<body>

<?php
$strr2 = “Visit Microsoft!”;
$patternr2 = “/microsoft/i”;
echo preg_replace($patternr2, “W3Schools”, $strr2);
?>

</body>
</html>

Date-and-Time

OutPut
Apr 01
Apr 08
Apr 15
Apr 22
Apr 29
May 06

<!DOCTYPE html>
<html>
<body>

<?php
$startdateD1=strtotime(“Saturday”);
$enddateD1=strtotime(“+6 weeks”, $startdateD1);

while ($startdateD1 < $enddateD1) {
echo date(“M d”, $startdateD1) . “<br>”;
$startdateD1 = strtotime(“+1 week”, $startdateD1);
}
?>

</body>
</html>

Cookie

OutPut
Cookie named 'user' is not set!

Note: You might have to reload the page to see the new value of the cookie.

<!DOCTYPE html>
<?php
$cookie_name = “user”;
$cookie_value = “Alex Porter”;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), “/”);
?>
<html>
<body>

<?php
if(!isset($_COOKIE[$cookie_name])) {
echo “Cookie named ‘” . $cookie_name . “‘ is not set!”;
} else {
echo “Cookie ‘” . $cookie_name . “‘ is set!<br>”;
echo “Value is: ” . $_COOKIE[$cookie_name];
}
?>

<p><strong>Note:</strong> You might have to reload the page to see the new value of the cookie.</p>

</body>
</html>

Filters

OutPut
Integer is valid

<!DOCTYPE html>
<html>
<body>

<?php
$int = 100;

if (!filter_var($int, FILTER_VALIDATE_INT) === false) {
echo(“Integer is valid”);
} else {
echo(“Integer is not valid”);
}
?>

</body>
</html>

Filters-Advanced

OutPut
Hello World!

<!DOCTYPE html>
<html>
<body>

<?php
// Variable to check
$strfd = “<h1>Hello WorldÆØÅ!</h1>”;

// Remove HTML tags and all characters with ASCII value > 127
$newstrfd = filter_var($strfd, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_HIGH);
echo $newstrfd;
?>

</body>
</html>

Exception

OutPut
Unable to divide. Process complete.

<!DOCTYPE html>
<html>
<body>

<?php
function divide($dividend, $divisor) {
if($divisor == 0) {
throw new Exception(“Division by zero”);
}
return $dividend / $divisor;
}

try {
echo divide(5, 0);
} catch(Exception $e) {
echo “Unable to divide. “;
} finally {
echo “Process complete.”;
}
?>

</body>
</html>

Callback Functions

OutPut
Digital Learining Goal! Digital Learining Goal?

<?php
function DLGex1($word) {
return $word. “! “;
}

function DLGex2($word) {
return $word. ;
}

function printFormatted($word, $format) {
// Calling the $format callback function
echo $format($word);
}

// Pass “DLGex1” and “DLGex2” as callback functions to printFormatted()
printFormatted(“Digital Learining Goal”, “DLGex1”);
printFormatted(“Digital Learining Goal”, “DLGex2”);
?>