Introduction to PHP basic and Programming Concepts
1.
PHP Basic
Prepared By:-
KaleJ. N.
Assistant Professor,
Computer Engineering,
Sanjivani College of Engineering, Kopargaon
Prepared By: Kale J. N.
2.
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.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.
5.
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.
6.
2.3 Comments inPHP
• Single Line:
// This is a comment
# This is also a comment
• Multi-line:
/* This is
multi-line comment */
Prepared By: Kale J. N.
7.
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.
8.
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.
9.
3.3 PHP DataTypes
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.
10.
3.4 Echo vsPrint
• echo "Hello";
• print "Hello";
• Difference:
• echo → faster, can print multiple
values
• print → returns 1, prints single
value
Prepared By: Kale J. N.
11.
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.
12.
• 3. StaticVariable
<?php
function counter() {
static $x = 0;
$x++;
echo $x;
}
counter();
counter();
?>
• Output:
1
2
Prepared By: Kale J. N.
13.
4. PHP Constants
•Defined using define()
<?php
define("PI", 3.14);
echo PI;
?>
• No $ sign
Prepared By: Kale J. N.
14.
5. PHP Loops
•Loops are used to execute code
multiple times.
Prepared By: Kale J. N.
15.
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.
16.
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.
17.
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.
18.
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.
19.
• With Keyand Value:
<?php
$student = array("name"=>"Jay",
"age"=>22);
foreach($student as $key => $value)
{
echo $key . " : " . $value;
}
?>
•
Prepared By: Kale J. N.