Intro D U C Tion To PHP: Chapter
Intro D U C Tion To PHP: Chapter
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 an open
source
3
WHAT IS A PHP FILE?
• PHP files may contain text, HTML tags and scripts
?>
◾ A PHP file normally contains HTML tags, and some PHP scripting
code.
5
COMMENTS IN PHP
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
?>
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.
◾ $var_name = value;
PHP VARIABLES
• A variable starts with the $ sign (a dollar sign ) , followed by the name of the variable
• 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
◾ 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;
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:
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:
◾ Example:
P H P Code : Output:
writeMsg(); // call
the function
?>
Thank You
20