0% found this document useful (0 votes)
16 views66 pages

Introduction to PHP Programming Basics

The document provides an introduction to PHP, a popular server-side scripting language used for creating dynamic web pages. It covers client-side and server-side programming, advantages of PHP, basic syntax, and how to set up a local web server. Additionally, it discusses PHP variables, their declaration rules, and comments in PHP.

Uploaded by

23-34981
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views66 pages

Introduction to PHP Programming Basics

The document provides an introduction to PHP, a popular server-side scripting language used for creating dynamic web pages. It covers client-side and server-side programming, advantages of PHP, basic syntax, and how to set up a local web server. Additionally, it discusses PHP variables, their declaration rules, and comments in PHP.

Uploaded by

23-34981
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

1

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

Prepared by: Bryan Emmanuel


Paz
Client-Side Programming
Refers to scripts that run in the user's web browser. The primary
purpose is to create a responsive and interactive user experience.
1
0
0Commonly includes HTML, CSS, and JavaScript. Frameworks and libraries
1
0like React, Angular, and [Link] are also widely used. 1
1 0
0 1
1Functionality: 0
0• 0
0
Manipulates the Document Object Model (DOM) to change content and 1
1 styles dynamically. 0
0 0
1
• Handles user interactions (like clicks and keyboard input). 1
0 0
1 1
0Advantages: 1
0
• Reduces server load since some processing is done on the client’s side. 1
• Provides a more interactive and responsive user experience. 1
1
Server-Side Programming
Involves scripts that run on the server before the content is sent to
the user's browser. It's responsible for managing and processing data,
handling business logic, and serving web pages.
1
0
Languages:
0 Common languages include PHP, Python, Ruby, Java, and [Link]
(JavaScript
1 on the server side). 1
0
1 0
1
0
Functionality
1 : 0
•0 Interacts with databases to retrieve, manipulate, and store data. 0
0 1
•1 Authenticates and authorizes users. 0
0
•01 Generates dynamic content based on user requests and server-side logic. 1
0 0
1
Advantages:
1
0 1
• Can handle complex processing and data management that cannot be 0
1
performed on the client side. 1
• Maintains a higher level of security since sensitive operations and data 1

are kept on the server.


Advantages of PHP over
Other Languages
• Easy to learn: PHP is easy to learn and use. For beginner programmers who just started
out in web development, PHP is often considered as the preferable choice of language to
learn.
1
0
0
• Open
1
source: PHP is an open-source project. It is developed and maintained by a
worldwide
0 community of developers who make its source code freely available to download1
1 0
and
0 use. 1
1 0
0 0
• Portability:
0 PHP runs on various platforms such as Microsoft Windows, Linux, Mac OS, 1
1 0
etc.
0
and it is compatible with almost all servers used today such Apache, IIS, etc. 0
1 1
0 0
• Fast
1 Performance: Scripts written in PHP usually execute or runs faster than those 1
written in other scripting languages like ASP, Ruby, Java, etc.
0 1
0
1
• Vast Community: Since PHP is supported by the worldwide community, finding help or 11
documentation related to PHP online is extremely easy.
WHAT IS PHP

• PHP stands for Hypertext Preprocessor. PHP is a very


1
popular and widely-used open-source server-side scripting
0
0
language to write dynamically generated web pages. PHP 1
1
0
was originally created by Rasmus Lerdorf in 1994. It was 01
1
0
initially known as Personal Home Page.
1
0
0
0
0 1
1 0
• PHP scripts are executed on the server and the result
0
1
0
1
is sent to the web browser as plain HTML. PHP can be
0
1
0
1
integrated with the number of popular databases, including
0 1
0

MySQL, PostgreSQL, Oracle, Microsoft SQL Server, Sybase, 1


1

and so on. 1
What is a PHP File?

• PHP files may contain text, HTML tags


and scripts.
1
0
0
1
0 1
1 0
0
1
0
• PHP files are returned to the browser as 1
0
0
0
1 plain HTML. 1
0
0
0
1 1
0 0
1
• PHP files have a file extension of ".php",
1
0 1
0

".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

A PHP script starts with the <?php and ends


1
0
0
with the ?> tag.
1
0 1
1 0
0 1
1 0
The PHP delimiter <?php and ?> in the
0
0
0
1
following example simply tells the PHP
1
0
0
0

engine to treat
1 1
0 0
1 1
the enclosed code block as PHP code, rather
0 1
0

than simple HTML 1


1
1
Basic PHP Syntax
In PHP, there are two basic ways to get output:
1
echo and print
0
0
echo and print are more or less the same. They are
1
0 1
both used to output data to the screen.
1
0
0
1
1 0
0 0
The differences are small: echo has no return value
0
1
1
0
while print has a return value of 1 so it can be
0
1
0
1
used in expressions. echo can take multiple
0
1
0
1
parameters (although such usage is rare) while
0 1
0

print can take one argument. echo is marginally 1


1

faster than print. 1


PHP echo and print
Statements
❮ PreviousNext ❯
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
PHP echo and print
Statements
The PHP echo Statement
The echo statement can be used with or without parentheses: echo or echo().
❮ PreviousNext ❯
The
1 PHP print Statement
0
The
0
print statement can be used with or without parentheses: print or print().
1
0 1
1 0
0<?php <?php 1
1 0
0 0
0echo "Hello World"; $result = print("Hello"); 1
1 0
0
echo ("Hello World"); echo $result; // Outputs 1 0
1 1
0echo "<br>"; 0
1 1
0 $result = "Hello"; 1
print "Hello World"; echo $result; 0
1
print ("Hello World"); 1
?> 1
?>
1
1
0
0
1 1
0 0
1 1
0 0
1

Setting Up a Local Web


1
0 0
1 1
1 0
1 1

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

• A variable name must start with a letter or an


1
0 underscore "_" -- not a number
0
1
0 1
1 • A variable name can only contain alpha-numeric 0
0 1
1 characters, underscores (a-z, A-Z, 0-9, and _ ) 0
0 0
0 1
1 0
0 • A variable name should not contain spaces. If a 0
1 1
0 variable name is more than one word, it should be 0
1 1
0 separated with an underscore ($my_string) or with 1
0
capitalization ($myString) 1
1
1
Variables in PHP
<?php
ECHO "Hello World!<br>"; PHP Case Sensitivity - In PHP,
echo "Hello World!<br>"; all user-defined functions,
1 EcHo "Hello World!<br>"; classes, and keywords (e.g. if,
0
$color="red"; else, while, echo, etc.) are NOT
0
1 echo "My car is " . $color . "<br>"; case-sensitive.
0 1
echo "My house is " . $COLOR . "<br>";
1
echo "My boat is " . $coLOR . "<br>"; However; in PHP, all variables 01
0
1 ?> are case-sensitive. 0
0 0
0 1
OUTPUT
1 All three echo statements below 0
0 0
1
are legal (and equal) 1
0 0
1 for the car display only the first 1
0 1
statement is valid(this is because0
$color, $COLOR, and $coLOR are 1
1
treated as three different 1
variables)
Comments in PHP - In PHP, we use // to make a single-line comment or /* and
*/ to make a large
1
comment
0 block.
0
1
0 <?php 1
1 0
0 1
1 //echo "Hello" this is single line comment ; 0
0 0
0 1
1 /* 0
0 echo "Welcome to PHP programming1"; 0
1 1
0 echo "Welcome to PHP programming2"; 0
1 this is multiple line comment 1
0 1
*/ 0
?> 1
1
1
PHP Concatenation
The concatenation operator (.) is used to put two string values together.
To concatenate two string variables together, use the concatenation operator:
1
0
0 <?php
1
0 1
1 $firstName = "Bryan"; 0
0 1
1
$secondName ="Emmanuel"; 0
0 $lastName = "Paz"; 0
0 $fullName = $firstName." ".$secondName." ".$lastName; 1
1 0
0 0
1 echo "Hello my First Name is $firstName <br>"; 1
0 0
1
echo "Hello my Second Name is $secondName"."<br>"; 1
0 echo 'Hello my FullName is '.$fullName.'^^'; 1
?> 0
1
1
1
Loosely typed language

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

type of a variable. PHP automatically determines the00


0 1
1
0
type of a variable based on the value assigned to it, 10
0
1

and it allows variables to change types dynamically 01


0
1
0 0
during script execution.
1
0
1
1
0
1
1
1
PHP 5 Data Types
String, Integer, Floating point numbers, Boolean, Array, Object, NULL.

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.

In1 PHP we have the following looping statements:


•0 while - loops through a block of code as long as a specified condition is true
•01 do...while - loops through a block of code once, and then repeats the loop as long as a special
0 condition is true 1
0
•1 for - loops through a block of code a specified number of times 1
0
•1 foreach - loops through a block of code for each element in an array 0
0 0
0 1
Looping
1 Basic Parameters 0
0 0
1
•10 init: Mostly used to set a counter (but can be any code to be executed once at the beginning of 0
1 the loop) 1
•0 condition: Evaluated for 1
each loop iteration. If it evaluates to TRUE, the loop continues. If it 0
evaluates to FALSE, the loop ends. 1
• increment: Mostly used to increment a counter (but can be any code to be executed at the end 1
1
of the loop)
PHP while Statement
Output:
The while
<?php statement will
$i=0; //init
1 while($i<=5) { execute a block
0
0 echo "The number is " . $i of code if and
. 1"<br />";
0
as long a 1
$i++; //increment
1
}0
condition is 0
1
1
?> true. 0
0 0
0 1
1 0
<?php
0 while 0
1 $i=5; //init 1
0
while($i>=0) { (condition) 0
1 1
0 print "The number is " . code to be 1
0
$i . "<br />"; executed; 1
$i--; //decrement 1
} 1
?>
The do...while Statement
<?php The do...while
$i=0; ; //init statement will
do {
1 $i++; //INCREMENT execute a block of
0
echo
0 "The number is " . $i . code at least once
"<br
1
0
/>"; - it then will 1
}while ($i<5);
1
?>
repeat the loop as01
0
1 long as a 0
0 0
0 condition is true.1
<?php
1 0
0 $i=5; ; //init 0
1
0
do { do { 1
0
1 $i--; //DECREMENT code to be 1
0 echo "The number is 1

" . $i . "<br />"; }


executed; 0
1
while ($i>0); } while 1
1
?> (condition);
The for Statement
<?php The for statement is
for ($i=1; $i<=5; $i++) { used when you know how
echo "Hello World # :
",$i,"!<br
1 />"; } many times you want to
0
?>
0
execute a statement or
1 a list of statements.
0 1
1 0
0<?php 1
1
for (initialization; 0
0
for ($i=5; $i>=0; condition; increment) { code 0
$i--)
0 { to be executed; } 1
1 echo "Hello World # : 0
0 0
",$i,"!<br
1 />"; } 1
0 ?> 0
1
Note:
1
0
The for statement has three parameters. The first parameter is for initializing 1
variables, the second parameter holds the condition, and the third parameter contains0
1
any increments required to implement the loop. If more than one variable is included in 1
either the initialization or the increment section, then they should be separated by 1

commas. The condition must evaluate to true or false.


The foreach Statement
<?php Loops over the array given
$arr=array("Naruto", "Sasuke", by the parameter. On each
"Sakura"); loop, the value of the current
1foreach ($arr as $value) { element is assigned to
0
0
echo "Value: " . $value . "<br $value and the array pointer
/>";
1 } is advanced by one - so on
0?> the next loop, you'll be 1
1 0
0 looking at the next 1
1 element. 0
0 0
0 foreach (array as value) { code to be1
1 executed; } 0
0 0
1 1
0 0
1
Loops over the array given by the parameter. On each loop, the value
1
0 1
0
of the current element is assigned to $value and the array pointer is 1
1
advanced by one - so on the next loop, you'll be looking at the next 1

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

You can create arrays by using the array() function:


You
1
0
can also use a shorter syntax by using the [] brackets:
0
1
0 1
1 0
0 1
1 0
0 0
0
1 $cars = array("Volvo", "BMW", "Toyota"); 1
0
0 0
1
0
$cars = ["Volvo", "BMW", "Toyota"]; 1
0
1 1
0 1
0
1
1
1
Multiple Lines

Line breaks are not important, so an array declaration can span


multiple
1
0
lines:
0
1
0 1
1 0
0 1
1 0
0
$cars = array( $cars = [ 0
0 "Volvo", "Volvo", 1
1 "BMW", "BMW", 0
0 0
1 "Toyota“ "Toyota“ 1
0); ]; 0
1 1
0 1
0
1
1
1
PHP Numeric Arrays
• A numeric array stores each array element with a numeric index.
• There are two methods to create a numeric array.
1
<?php
0
0 //using Foreach
1 $cars=array("Juan", "Pedro",
0 "Fernando"); 1
1 0
0 1
echo "Using for each<br>"; 0
1
0 foreach ($cars as $value){ 0
0 echo "Value: ". $value . "<br />"; 1
1 0
0 } 0
1 1
0 0
1 //using loop 1
0 echo "<hr>Using Loop<br>"; 1
for ( $x = 0; $x<=2; $x++){ 0
echo "Car:[$x] " . $cars[$x] . "<br 1
1
/>"; 1
}
?>
PHP Associative Arrays
• With an associative array, each ID key is associated with a value.
• When storing data about specific named values, a numerical array is not always the best way to do it.
•1 With associative arrays we can use the values as keys and assign values to them.
0
In 01this example we use an array to assign ages to the different persons:
0 1
1 $ages=array("Pedro"=>20,"Juan"=>22,"Fernando"=>32); 0
0 var_dump($ages); 1
1 0
0 0
0This example is the same as the one above, but shows a different way of creating the array: 1
1 0
0 0
1$ages["Pedro"]="20"; 1
0 0
1
$ages["Juan"]="22"; 1
0$ages["Fernando"]="32"; 1
0
1
Then the ID keys can be used in a script to display the data 1
1
• $ages["Pedro"];
PHP Associative Arrays
$ages=array("Pedro"=>20,"Juan"=>21,"Fernando"=>32);

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

You might also like