Skip to main content
PHP Basic
Prepared By:-
Kale J. N.
Assistant Professor,
Computer Engineering,
Sanjivani College of Engineering, Kopargaon
Prepared By: Kale J. N.
1. PHP Introduction
• PHP stands for Hypertext Preprocessor
• It is a server-side scripting language
• Used to develop dynamic web applications
• PHP code runs on the server and sends output as HTML to the
browser
• A PHP file has extension: .php
Prepared By: Kale J. N.
2. PHP Basic Syntax
Prepared By: Kale J. N.
2.1 PHP Tags
• PHP code must be written
inside:
<?php
// PHP code here
?>
• Example:
<?php
echo "Hello World!";
?>
• echo is used to print output.
• Every statement ends with ;
Prepared By: Kale J. N.
2.2 Case Sensitivity
• Variables are case-sensitive
$name = "Arav";
echo $Name; // Error
• Keywords are NOT case-
sensitive
• ECHO "Hello";
• echo "Hello";
Prepared By: Kale J. N.
2.3 Comments in PHP
• Single Line:
// This is a comment
# This is also a comment
• Multi-line:
/* This is
multi-line comment */
Prepared By: Kale J. N.
3. PHP Variables
• 3.1 Rules for Variables
• Must start with $
• After $, the first character must be
a letter (a–z, A–Z) or underscore
(_)
• Cannot start with number
• No spaces allowed
• Case-sensitive
• Valid Examples:
$name = "Sai";
$_age = 25;
$marks1 = 90;
Prepared By: Kale J. N.
3.2 Declaring Variables
<?php
$name = "Sai";
$age = 22;
$marks = 85.5;
$isStudent = true;
?>
• PHP automatically decides the
datatype (dynamically typed
language ).
Prepared By: Kale J. N.
3.3 PHP Data Types
Data Type Example
String "Hello"
Integer 10
Float 10.5
Boolean true / false
Array array(1,2,3)
Object Class object
NULL null
• Example:
<?php
$x = 10; // Integer
$y = 5.5; // Float
$name = "Jay"; // String
$isPass = true; // Boolean
?>
Prepared By: Kale J. N.
3.4 Echo vs Print
• echo "Hello";
• print "Hello";
• Difference:
• echo → faster, can print multiple
values
• print → returns 1, prints single
value
Prepared By: Kale J. N.
3.5 Variable Scope
• 1. Local Scope
<?php
function test() {
$x = 10;
}
echo $x; // Error
?>
• 2. Global Scope
<?php
$x = 10;
function test() {
global $x;
echo $x;
}
test();
?>
Prepared By: Kale J. N.
• 3. Static Variable
<?php
function counter() {
static $x = 0;
$x++;
echo $x;
}
counter();
counter();
?>
• Output:
1
2
Prepared By: Kale J. N.
4. PHP Constants
• Defined using define()
<?php
define("PI", 3.14);
echo PI;
?>
• No $ sign
Prepared By: Kale J. N.
5. PHP Loops
• Loops are used to execute code
multiple times.
Prepared By: Kale J. N.
5.1 While Loop
• Syntax:
while(condition) {
// code
}
• Example:
<?php
$i = 1;
while($i <= 5) {
echo $i;
$i++;
}
?>
• Output:
12345
Prepared By: Kale J. N.
5.2 Do-While Loop
• Executes at least once
• Syntax:
do {
// code
} while(condition);
• Example:
<?php
$i = 1;
do {
echo $i;
$i++;
} while($i <= 5);
?>
Prepared By: Kale J. N.
5.3 For Loop
• Used when number of iterations
is known.
• Syntax:
for(initialization; condition;
increment) {
// code
}
• Example:
<?php
for($i=1; $i<=5; $i++) {
echo $i;
}
?>
Prepared By: Kale J. N.
5.4 Foreach Loop
• Used for arrays.
• Syntax:
foreach($array as $value) {
// code
}
• Example:
<?php
$colors = array("Red", "Green",
"Blue");
foreach($colors as $color) {
echo $color;
}
?>
Prepared By: Kale J. N.
• With Key and Value:
<?php
$student = array("name"=>"Jay",
"age"=>22);
foreach($student as $key => $value)
{
echo $key . " : " . $value;
}
?>
•
Prepared By: Kale J. N.
6. Loop Control Statements
Prepared By: Kale J. N.
6.1 Break
• Stops loop immediately.
<?php
for($i=1; $i<=10; $i++) {
if($i == 5) {
break;
}
echo $i;
}
?>
• Output:
1234
Prepared By: Kale J. N.
6.2 Continue
• Skips current iteration.
<?php
for($i=1; $i<=5; $i++) {
if($i == 3) {
continue;
}
echo $i;
}
?>
• Output:
1245
Prepared By: Kale J. N.
7. Nested Loops
• Loop inside another loop.
<?php
for($i=1; $i<=3; $i++) {
for($j=1; $j<=2; $j++) {
echo $i . $j . " ";
}
}
?>
• Output:
11 12 21 22 31 32
Prepared By: Kale J. N.