Introduction to PHP Programming Basics
Introduction to PHP Programming Basics
0
1
0
0
1
0
Introducti
1
1 0
0 1
0 0
1 0
on to PHP
0 1
1 0
0 1
1 0
0
1 #Start
0
1
0
1
0
0
and so on. 1
What is a PHP File?
".php3", or ".phtml"
1
1
1
1
1
0
0
1 1
0 0
1 1
0 0
PHP
1 1
0 0
1 1
1 0
1 1
Syntax
0 1
0 1
0 0
1
1
1
0
Basic PHP Syntax
engine to treat
1 1
0 0
1 1
the enclosed code block as PHP code, rather
0 1
0
Server
0 1
0 1
0 0
1
1
1
0
Setting Up a Local Web
Server
PHP script execute on a web server running PHP. So, before
We start writing any PHP program you need the following
program
1
0
installed on your computer.
0
1
0 1
1 • The Apache Web server 0
0 1
1 0
0
0
0
• The PHP engine 1
1 0
0 0
1 • The MySQL database server 1
0 0
1 1
0 1
You can either install them individually or choose a pre-configured 0
1
package for your operating system like Linux and Windows. Popular 1
1
pre-configured package are XAMPP and WampServer.
Setting Up a Local Web
Server
Download Here:
[Link]
1
0
0
1
0 1
1 0
0 1
1 0
0 0
0 1
1 0
0 0
1 1
0 0
1 1
0 1
0
1
1
1
Example. Follow the steps
below.
Step 1. Create a document in your text editor. Step 3. Save the file as [Link]
Step 2. Type the code as shown below Since PHP is a server-side
programming language, you need to
<html>
1
save it in XAMPP directory. Basically, it
0
<body>
0 is in your c drive, folder xampp, then
1 go to your htdocs folder.
0 1
1 0
<?php echo "Welcome to PHP programming";
0 1
?>
1 0
0 Take note of the extension name. it is 0
</body>
0 1
</html>
1 now php and not html. 0
Each
0 code line in PHP must end with a 0
1 1
semicolon.
0
The semicolon is a separator and is 0
used
1 to distinguish one set of instructions from 1
another.
0 1
0
In PHP, variable and constant names are 1
1
case sensitive, while function names are 1
not
Example. Follow the steps
below. Step 4. View your php program in the
browser by typing
1 [Link]
0
0
1
Localhost is your host name or server
0 name, if you are running the program1in
1 a server. [Link] is your php program0
0 1
1 0
0 Note. instead of host name, you can also
0
0 1
1
your ip address. 0
0 0
1 Like 1
0 0
1 [Link]
0 1
0
1
1
1
1
1
0
0
1 1
0 0
1 1
0 0
PHP Variables
1 1
0 0
1 1
1 0
1 1
0 1
0 1
0 0
1
1
1
0
Variables in PHP
• Variables are "containers" for storing
1
information like text strings, numbers or arrays
0
0
• All variables in PHP start with a $ sign symbol.
1
0 1
1 0
0 1
• In PHP, a variable does not need to be declared
1
0
0
0
0
1 before adding a value to it. 1
0
0 0
1 1
• PHP automatically converts the variable to the
0
1
0
1
0
correct data type, depending on its value. 1
0
1
1
1
Rules in Declaring Variables
1
0
PHP is a loosely typed language. This means
0
1
that you do not need to explicitly declare the data 10
0
1
The PHP var_dump() function returns the data type and value of variables:
1
0
<?php
0
1
0 $x = 10.365; 1
1 0
0 var_dump($x); // output is float(10.365) 1
1 echo "<br>"; 0
0 0
0
$x = 2.4e3; 1
1 var_dump($x); // output is float(2400) 0
0 echo "<br>"; 0
1 1
0 $x = 8E-5; 0
1 var_dump($x); // output is float(8.0E-5) 1
0 1
echo "<br>"; 0
$x = "BSU"; 1
var_dump($x); // output is string(3) 1
1
“BSU”
?>
1
1
0
0
1 1
0 0
1 1
0 0
PHP Operators
1 1
0 0
1 1
1 0
1 1
0 1
0 1
0 0
1
1
1
0
Arithmetic Operators
1
0
0
1
0 1
1 0
0 1
1 0
0 0
0 1
1 0
0 0
1 1
0 0
1 1
0 1
0
1
1
1
Assignment Operators
1
0
0
1
0 1
1 0
0 1
1 0
0 0
0 1
1 0
0 0
1 1
0 0
1 1
0 1
0
1
1
1
Comparison Operators
1
0
0
1
0 1
1 0
0 1
1 0
0 0
0 1
1 0
0 0
1 1
0 0
1 1
0 1
0
1
1
1
Logical Operators
1
0
0
1
0 1
1 0
0 1
1 0
0 0
0 1
1 0
0 0
1 1
0 0
1 1
0 1
0
1
1
1
Example
$x=120; $y=120;
1
0
0 $diff=$x-$y;
1
0 1
1 echo "the sum is : ".($y+$x); 0
0 1
echo "<br><div class='bb'>the product is</div> : ".($y*$x); 0
1
0 echo "<br>the quotient is : ".($y/$x); 0
0 echo "<br>the difference is: $diff"; 1
1 0
0 echo "<hr>"; 0
1 $x = 'r'; 1
0 0
$y = 1; 1
1
0 echo "the sum is : ".($y+$x); //output is 1. it will ignore $x 1
0
1
1
1
1
1
0
0
1 1
0 0
1 1
0 0
1 1
Exercise 1
0 0
1 1
1 0
1 1
0 1
(operators)
0 1
0 0
1
1
1
0
Exercise
Write a PHP script that introduces a person named Kazuto, who is currently 22
years old. The script should also display the following:
1
0
•
0
1
Kazuto's birth year.
•
0 Whether Kazuto is an adult. 1
0
1
•
0 How old Kazuto will be next year. 1
1 0
•
0 How many years Kazuto has until they turn 30. 0
0 1
•
1 Whether Kazuto is a teenager. 0
•
0
1
A sentence about Kazuto's hobby (graphic designing) and future goal 0
1
0
1
(becoming a front-end web developer). 0
1
0 1
0
Use appropriate variables for the name, age, hobby, and goal, and include 1
1
basic conditional statements to determine adult status and teenage status. 1
Exercise output
1
0
0
1
0 1
1 0
0 1
1 0
0 0
0 1
1 0
0
Hi, I am Kazuto. I am currently 13 years old. 0
1 My birth year is 2011. 1
0 0
I am not yet an adult. 1
1
0 Next year, I will be 14 years old. 1
I have 17 years until I turn 30. 0
1
I am a teenager. 1
I love graphic designing and want to become a front-end web developer in the future. 1
1
1
0
0
1 1
0 0
1 1
0 0
1 1
Conditional
0 0
1 1
1 0
1 1
0 1
Statements
0 1
0 0
1
1
1
0
Conditional Statements
Very often when you write code, you want to perform different
actions for different decisions. You can use conditional
statements
1
0
in your code to do this
0
1
0
In PHP we have following conditional statements: 1
1 0
0 1
1 • if statement - use this statement to execute some code only 0
0 0
0 if a specified condition is true 1
0
1
0
• if...else statement - use this statement to execute some 0
1
0
code if a condition is true and another code if the condition is 1
0
1 false 1
1
0
• if...else if....else statement - use this statement to select 0
1
one of several blocks of code to be executed 1
1
• switch statement - use this statement to select one of
many blocks of code to be executed
PHP IF STATEMENT
<!DOCTYPE html>
<html lang="en">
<head> if (condition)
1
0
<meta charset="UTF-8"> code to be executed if
<meta name="viewport"
0
content="width=device-width,
1 condition is true;
0 1
initial-scale=1.0"> 0
1
0 <title>Web and 1
System</title>
1 0
0 0
</head>
0 Output: 1
<body>
1 0
0 0
<?php 1
1
0 $grade=80; 0
1 if ($grade>=75 1
0 1
echo "Passed!"; 0
?> 1
1
</body> 1
</html>
PHP IF-ELSE STATEMENT
<!DOCTYPE html>
<html lang="en">
<head> if (condition)
1 <meta charset="UTF-8"> code to be executed if condition is
0 <meta name="viewport"
0
content="width=device-width, true;
1
initial-scale=1.0">
0 else 1
0
1 <title>Web and
0 code to be executed if condition is 1
System</title> 0
1
</head>
0
false; 0
<body>
0 1
1 0
<?php Output: 0
0
1 $grade=74; 1
0 if ($grade>=75) 0
1 1
echo "Passed!"; 1
0
else 0
echo "Failed 1
?> 1
1
</body>
</html>
PHP ELSE-IF STATEMENT
<!DOCTYPE html> if (condition)
<html lang="en">
code to be executed if condition is
<head>
1
<title>Web and true;
0
System</title>
0 else if(condition)
</head>
1
0
<body> code to be executed if condition is 1
1 0
0 <?php true; 1
1
0
$grade=100; else 0
0
0 if ($grade>=90) code to be executed if condition is 1
1 echo "Excellent!"; 0
0
false;
Output: 0
1 elseif ($grade>=75) 1
0 echo "Not bad!"; 0
1 1
0
else 1
echo "Failed!"; 0
?> 1
1
</body> 1
</html>
The Switch Statement
If you want to select one of many blocks of code to be executed, use the switch
statement.
1
0
switch
0 (expression) $x = "4";
{ 1
0
switch ($x) { 1
case
1 label1: case 1: 0
code
0 to be executed if echo "Number 1"; 1
1 0
expression
0
= label1; break; 0
break;
0 case 2: 1
1 0
case label2: echo "Number 2"; 0
0
code
1 to be executed if break; 1
expression
0 = label2; default: 0
1 1
break;
0 echo "No number between 1 and 1
default: 2"; 0
1
code to be executed } 1
if expression is different 1
from both label1 and label2;
}
1
1
0
0
1 1
0 0
1 1
0 0
Loops
1 1
0 0
1 1
1 0
1 1
0 1
0 1
0 0
1
1
1
0
PHP Looping
Very often when you write code, you want the same block of code to run a number of times. You can
use looping statements in your code to perform this.
element.
1
1
0
0
1 1
0 0
1 1
0 0
PHP
1 1
0 0
1 1
1 0
1 1
Arrays
0 1
0 1
0 0
1
1
1
0
PHP Arrays
1
0• An array variable is a storage area holding a
0
1
0
number or text. The problem is, a variable will hold 1
1
0
only one value. 0
1
1 0
0 0
0 1
1
0
• An array is a special variable, which can store 0
0
1
0multiple values in one single variable. 1
0
1 1
0 1
0
1
1
1
In PHP, there are three
kinds of arrays:
1
0
0
1
0
1. Numeric array - An array with a numeric index . 1
1 0
0
1
2. Associative array - An array where each ID key is 1
0
0 0
0
1
associated with a value. 1
0
0 0
1
0
3. Multidimensional array - An array containing one or more 1
0
1 1
0 arrays. 1
0
1
1
1
Create Array
1
echo 'Pedro age is: '.$ages["Pedro"]; //display age of pedro which is
0
20
0
echo
1 '<br>';
echo
0 '<hr>loop and display the keys only<hr>'; 1
1 0
0 1
$keys = array_keys($ages); 0
1
0 0
foreach($keys
0 as $key) { 1
1 echo($key). "<br>"; 0
}0 0
1 1
0 0
1 1
echo
0 '<hr>loop with it and display the value<hr>’; 1
0
1
1
foreach($ages as $key => $value) { 1
echo($key). " " . $value . "<br>";
}
PHP Multidimensional
Arrays
• A multidimensional array is an array containing one or more
arrays.
1
0
0
1
• PHP supports multidimensional arrays that are two, three, four,
0 five, or more levels deep. However, arrays more than three 1
0
1
0 levels deep are hard to manage for most people. 1
1 0
0 0
0 1
1 The dimension of an array indicates the number of indices you 0
0 0
1
need to select an element. 1
0 0
1 1
0• For a two-dimensional array you need two indices to select an element 1
• For a three-dimensional array you need three indices to select an 0
1
element 1
1
PHP - Two-dimensional
Arrays
A two-dimensional array is an array of arrays (a three-dimensional array is an array
of arrays of arrays).
First, take a look at the following table:
1
0
0
1
0 1
1 0
0 1
1 0
0 0
0 1
1 We can store the data from the table above in a two-dimensional array, like this: 0
0 0
1$cars = array ( 1
0 0
1
array("Volvo",22,18), 1
0 array("BMW",15,13), 1
0
array("Saab",5,2), 1
array("Land 1
Rover",17,15) 1
);
PHP - Two-dimensional
Arrays
Now the two-dimensional $cars array contains four arrays, and it has two indices:
row and column.
To get access to the elements of the $cars array we must point to the two indices
(row and column):
1
0
0
1
echo
0 $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";1
echo
1 $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";0
0 1
echo
1 $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";0
echo
0 $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";0
0 1
1 0
0 0
1 1
0 0
1 1
0 1
0
1
1
1
PHP - Two-dimensional
Arrays
We can also put a for loop inside another for loop to
get the elements of the $cars array (we still have to
point
1 to the two indices):
0
0 <?php
1
0 $cars = array ( 1
1 array("Volvo",22,18), 0
0 array("BMW",15,13), 1
1 array("Saab",5,2), 0
0 array("Land Rover",17,15) 0
0 ); 1
1 0
0 for ($row = 0; $row < 4; $row++) { 0
1 1
echo "<p><b>Row number
0 0
$row</b></p>";
1 1
0 1
for ($col = 0; $col < 3; $col+ 0
+) { 1
echo $cars[$row] 1
[$col]."<br>"; 1
}
}
PHP - Two-dimensional
Arrays
$cars = array (
array("Volvo",22,18),
1 array("BMW",15,13),
0 array("Saab",5,2),
0 array("Land Rover",17,15)
1 );
0 1
1 echo "<table border='1' cellpadding='10'>"; 0
0 echo 1
1 "<tr><th>Name</th><th>Stock</th><th>Sold</th></t 0
0 r>"; 0
0 1
1 for ($row = 0; $row < 4; $row++) { 0
0 echo "<tr>"; 0
1 for ($col = 0; $col < 3; $col++) { 1
0 echo "<td>".$cars[$row][$col]."</td>"; 0
1 } 1
0 echo "</tr>"; 1
} 0
echo "</table>"; 1
1
1
?>
1
1
0
0
1 1
0 0
1 1
0 0
Functio
1 1
0 0
1 1
1 0
1 1
ns
0 1
0 1
0 0
1
1
1
0
PHP Functions – User
Defined
Besides the built-in PHP functions, it is possible to create your own functions.
• Besides the built-in PHP functions, we can create our own functions.
•10 A function is a block of statements that can be used repeatedly in a program.
•0 A function will not execute immediately when a page loads. You need to call it.
•1 A function will be executed by a call to the function.
0 1
1 0
0 function writeMsg(){ 1
1 0
0 echo "Hello world!"; 0
0 } 1
1
0 Syntax: 0
0
1 writeMsg(); // call the function 1
0 ?> 0
1 1
0 function functionName() { 1
Code to be executed; 0
1
} Output: 1
1
Function with Arguments
To add more functionality to a function, we can add parameters. A parameter
is just like a variable.
Parameters
1
are specified after the function name, inside the parentheses.
0
0
1
0 1
1 <?php Output: 0
0 1
1 0
0
function familyName($fname) { 0
0 echo "$fname .<br>"; 1
1 } 0
0 0
1 1
0 familyName("Naruto"); 0
1 1
0
familyName("Boruto"); 1
familyName("Himawari"); 0
familyName("Hinata"); 1
1
?> 1
Function with Multiple
Arguments
The following example has a function with two arguments
($fname, $year):
1
0
0
1
0 1
1 <?php Output: 0
0 function deathNote($fname, $year) { 1
1 0
0 echo "$fname $year <br>"; 0
0 } 1
1 0
0 0
1 deathNote("Light", "1999"); 1
0 deathNote("Ryuk", "1878"); 0
1 1
0 deathNote("Near", "2022"); 1
?> 0
1
1
1
PHP Default Argument Value
The following example shows how to use a default parameter. If we call the function setHeight()
without arguments it takes the default value as argument:
1
0
0
1 <?php
0 1
1 0
0 function setHeight($minheight = 50) { 1
1 0
0
echo "The height is : $minheight <br>"; 0
0 } 1
1 0
setHeight(350); 0
0
1 setHeight(); // will use the default value of 50 1
0 setHeight(135); 0
1 1
0
setHeight(80); 1
?> 0
1
1
1
PHP Functions - Returning
values
The following example shows how to use a default parameter. If we call the function setHeight()
without arguments it takes the default value as argument:
1
0
0
1 <?php
0 1
1 0
0
function sum($x,$y) { 1
1 $z=$x+$y; 0
0 0
return $z; 1
0
1 } 0
0 0
1 1
0
echo "7 + 13 = " . sum(7,13) . "<br>"; 0
1 echo "2 + 4 = " . sum(2,4); 1
0 1
0
?> 1
1
1
Variable Number of Arguments
By using the... operator in front of the function parameter, the function accepts an unknown
number of arguments. This is also called a variadic function.
1The variadic function argument becomes an array.
0
0
1 function sumMyNumbers(...$numbers) { Output:
0 $total = 0; 1
1 0
$len = count($numbers);
30
0 1
1 for($i = 0; $i < $len; $i++) { 0
0 0
0
$total += $numbers[$i]; 1
1 } 0
0 return $total; 0
1 1
0 } 0
1 1
0 1
$sum = sumMyNumbers(5, 5, 5, 5,5, 5); 0
echo $sum; 1
1
1
Variable Local and Global
Scope
• A variable declared outside a function has a GLOBAL SCOPE and can only be accessed outside a
function.
• A variable declared within a function has a LOCAL SCOPE and can only be accessed within that
function.
1
0
0
PHP
1 The global Keyword <?php
0 1
1 0
0 $x=5; 1
1 $y=10; 0
0• The global keyword is used to function myTest() { 0
0access a global variable from within a global $x,$y; 1
1 0
$y=$x+$y;
0function. 0
1 } 1
0 0
• To do this, use the global keyword 1
1 myTest();
0before the variables (inside the echo $y; // outputs 15 1
function): 0
1
?> 1
1