1.PHP Basics
1.PHP Basics
PHP
•Hypertext Preprocessor
•serverside scripting languages for creating dynamic Web pages
•PHP files have extension ".php“
•PHP can be easily embedded in HTML files and HTML codes can also be
written in a PHP file
Why PHP
•Platform independent(Windows, Linux, Unix, Mac OS X, etc.)
•PHP is compatible with almost all servers used today (Apache, IIS, etc.)
•PHP supports a wide range of databases including mysql
PHP
PHP SYNTAX
•A PHP script is executed on the server, and the plain HTML result is
sent back to the browser."
<?php
// PHP code goes here
?>
A PHP file normally contains HTML tags, and some PHP scripting code.
A PHP script can be placed anywhere in the document.
PHP statements end with a semicolon (;)
PHP EXAMPLE
<!DOCTYPE html>
<html>
<body>
<h1>PHP page</h1>
<?php
echo "Hello"; ends with a semicolon
?>
</body>
</html>
PHP CASE INSENSITIVE
In PHP,
• keywords (e.g. if, else, while, echo, etc.),
• classes,
• functions
• user-defined functions are not case-sensitive.
PHP COMMENTS
• A variable starts with the $ 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 (A-z,
0-9, and _ )
• Variable names are case-sensitive ($age and $AGE are two different variables)
• PHP is a loosely typed language, and we do not require to declare the data types of
variables, rather PHP assumes it automatically by analyzing the values.
Example : $age,$name
CONSTANTS
• Constants are either identifiers or simple names that can be assigned any
fixed values.
• They are similar to a variable except that they can never be changed
• Constants are global
•A There is no need to use dollar sign($) before constants during assignment
•A constant can only be defined using a define() function and not by any
simple assignment.
<?php
define("WELCOME", “PHP");
echo WELCOME, "\n";
?>
CONSTANTS
constant() function
another way to print constants using the constant() function.
Syntax
constant(name)
<?php
define("WELCOME", “PHP");
echo constant("WELCOME");
?>
DATA TYPES
Predefined (or)
$x = null;
$x = true;
$val = 50;
stores
$arr = array( 10, 20 , 30);
$val = 50.85; reference to
functions and
class sample
resources
{}
$name = "Krishna"; external to
$obj=new sample(“obj1”);
PHP(db)
DATA TYPES
var_dump() function returns the data type and value
<?php
$modules =
array("HTML","CSS","Javascript");
var_dump($modules);
?>
array(3) { [0]=> string(4) "HTML" [1]=> string(3) "CSS" [2]=> string(10) "Javascript" }
OPERATORS
OPERATORS
Arithmetic
OPERATORS
comparison Increment/decrement Logical
-1,if $x < $y
0, if $x = $y
+1, if $x > $y
OPERATORS
string Array
<?php
if (condition) $x = -12;
if ($x > 0)
{
{
// if TRUE then execute this code echo "The number is positive";
} }
else{ else
{
// if FALSE then execute this code
echo "The number is negative";
} }
?>
DECISION MAKING
if (condition) <?php
{ // if TRUE then execute this code } $x = "August";
elseif if ($x == "January") {
{ // if TRUE then execute this code } echo "Happy Republic Day";
elseif }
{ // if TRUE then execute this code }
else elseif ($x == "August") {
{ // if FALSE then execute this code } echo "Happy Independence Day!!!";
}
else{
echo "Nothing to show";
}
?>
DECISION MAKING
<?php
switch(n) $n = "February";
{ case statement1: switch($n) {
code to be executed if n==statement1; case "January":
break; echo "Its January";
case statement2: break;
code to be executed if n==statement2; case "February":
break; echo "Its February";
case statement3: break;
code to be executed if n==statement3; case "March":
break; echo "Its March";
case statement4: break;
code to be executed if n==statement4; case "April":
break; ...... echo "Its April";
default: break;
code to be executed if n != any case; default:
} echo "Doesn't exist";
}
?>
LOOPING
for (initialization expression;test condition;update expression)
{ // code to be executed }
<?php
?>
LOOPING
while (if the condition is true)
{ // code is executed }
<?php
$num = 2;
?>
LOOPING
do {
//code is executed
}
while (if condition is true);
<?php
$num = 2;
do {
$num += 2;
echo $num, "\n";
} while ($num < 12);
?>
LOOPING
• used to iterate over arrays
• For every counter of loop, an array element is assigned and the next counter is shifted to the next
element
<?php
$arr = array (10, 20, 30, 40, 50, 60);
foreach ($arr as $val) {
echo "$val \n";
}
$arr = array ("Ram", "Laxman", "Sita");
foreach ($arr as $val) {
echo "$val \n";
}
?>