Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

University of Bisha

Faculty of Computing and Information Technology

Course title and code: Web Development(IT-541)

Chapter-
INTRO D U C TIO N TO PH P

1
Chapter Contents
◾ What PHP?
◾ Basic PHP Syntax
◾ Comments echo and print Statements
◾ Creating (Declaring) Variables
◾ PHP Arrays
◾ Conditional Statements ( if – else )
◾ for Loop
◾ foreach Loop
◾ Functions

2
WHAT IS PHP?
• PHP stands for PHP: Hypertext Preprocessor

• PHP is a server-side scripting language

• PHP scripts are executed on the server

• PHP supports many databases like (MySQL, Informix, Oracle, Sybase,


Solid, PostgreSQL, Generic ODBC, etc.)

• PHP is an open
source

3
WHAT IS A PHP FILE?
• PHP files may contain text, HTML tags and scripts

• PHP files are returned to the browser as plain HTML

• PHP files have a file extension of ".php", ".php3", or ".phtml"


BASIC
BASICPHP SYNTAX
PHP SYNTAX

◾ A PHP script can be placed anywhere in the


document.

◾ A PHP script starts with <?php and ends with


?>:
<?php

// PHP code goes here

?>

◾ The default file extension for PHP files is ".php".

◾ A PHP file normally contains HTML tags, and some PHP scripting
code.

5
COMMENTS IN PHP

◾PHP support three kinds of comment tags : 1. //


◾ This is a one-line comment

2. #
◾ This is a Unix shell-style comment. It's also a one-line comment

3. /* ..... */
◾ Use this multi line comment if you need to.
COMMENTS IN PHP - EXAMPLES
PHP ECHO AN D PRINT STATEMENTS

◾ echo and print are both used to output data to the screen.
◾ They can be used with or without parentheses: echo or echo()
◾ Example: print or print()

<?php

echo "<h2> Hello world!

</h2>"; print "Hello world!";

print ("Hello world! ");

?>

8
CREATING (DECLARING) PHP VARIABLES

• Variables are used for storing values, like text strings, numbers or arrays, etc..

• When a variable is declared, it can be used over and over again in your script.

• All variables in PHP start with a $ sign symbol.

• The correct way of declaring a variable in PHP:

◾ $var_name = value;
PHP VARIABLES

◾ Rules for PHP variables:

• A variable starts with the $ sign (a dollar sign ) , followed by the name of the variable

• A variable name must start with a letter or the underscore character

• A variable name cannot start with a number

• A variable name can only contain alpha-numeric characters and underscores _

• Variable names are case-sensitive ($age and $AGE are two different variables)
PHP VARIABLES : EXAMPLE
P H P Code : Output:
<?php Hello world!
$txt = "Hello world!"; 5
$x = 5; 10.5
$y = 10.5; Sum of : 5 +
$z= $x + $y; 10.5 = 15.5

echo $txt;
echo "<br>";
echo $x; Note: To concatenate two or
echo "<br>"; more variables together, use the
dot (.) operator
echo $y;
echo "<br>";
echo "Sum
of : " . $x. "
+ " .$y. " =
" .$z ;
?>
PHP ARRAYS
◾ An array is a special variable, which can hold more than one value at a time.

P H P Code : Output:
Mer
<?php cede
$cars = array("Mercedes", "BMW", s
BM
"Toyota"); W
Toyot
a

$arrlength = count($cars);
for($x = 0; $x < $arrlength; $x++) {
echo $cars[$x];
echo "<br>";
}
?>
PHP CONDITIONAL STATEMENTS

◾ PHP - The if Statement


◾ The if statement executes some code if one condition is true.

◾ Syntax:

if (condition) {
code to be executed if condition is true;
}
PHP - THE IF STATEMENT : EXAMPLE
P H P Code : Output:
<?php x is bigger than y

$x=10;
$y=8;

if ($x > $y)


{
echo "x is bigger than y";
}

else
{
echo "x is smaller than y";
}
?>
PHP FOR LOOP

◾ The for loop - Loops through a block of code a specified number of times.

◾ Syntax:

for (init counter; test counter; increment counter) {


code to be executed for each iteration;
}

Parameters:
 init counter: Initialize the loop counter value
 test counter: Evaluated for each loop iteration. If it evaluates to TRUE, the loop
continues. If it evaluates to FALSE, the loop ends.
 increment counter: Increases the loop counter value

15
THE PHP FOR LO O P : EXAMPLE
◾ The example below displays the numbers from 0 to 10:

P H P Code : Output:
<?php The number is: 0
for ($x = 0; $x <= 10; $x++) { The number is: 1
echo "The number is: $x <br>"; The number is: 2
} The number is: 3
?> The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10

16
THE PHP FOREACH LOOP
The foreach loop works only on arrays, and is used to loop through each key/value pair in an array.

Syntax:

foreach ($array as $value) {


code to be executed;
}
THE PHP FOREACH LOOP: EXAMPLE:
P H P Code : Output:
<?php red
green
$colors blue
= array("red", "green", "blue", "yell yellow
ow");

foreach ($colors as $value) {


echo "$value <br>";
}
?>
PHP FUNCTIONS
◾ Syntax:
function functionName() {
code to be executed;
}

◾ Example:
P H P Code : Output:

<?php Hello world!


function
writeMsg()
{ echo "Hello
world!";
}

writeMsg(); // call
the function
?>
Thank You

20

You might also like