Module 3
Module 3
A Variable is simply a
container i.e used to store both numeric and non-numeric information.
• Variables in PHP starts with a dollar($) sign, followed by the name of the
variable.
• The variable name must begin with a letter or the underscore character.
• A variable name can only contain alpha-numeric characters and underscores (A-z,
0-9, and _) Standard Naming Convention
$firstNumber = 90;
• A variable name should not contain space $myFirstNumber = 90; $personalInfo
$first_number =
This assign value on the right side of the equation to the variable on the left.
Example 1
<?php
$myCar = "Honda";
echo $myCar;
?>
Output Honda
In the above example Create a variable ($mycar) containing a string with
value="Honda". To print the carname pass $mycar inside echo statement.
PHP Concatenation
Example 2 (concatenate variable with string)
<?php
?>
<?php
$first = 100;
$second = 200;
?>
<?php
$first = 1000;
$second = 500;
$third = $first - $second;
?>
Example 5
<?php
$name="steve";
echo $name;
unset($name);
?>
Output steve
In the above example declare variable $name hold value="steve". In this program we
used unset() function to delete a particular variable. first it shows the output: "steve",
because we pass unset function after echo statement. Now pass variable name inside
unset($name) function output will show an Notice error(Variable is undefined).
Example 6
<?php
$first = 100;
$second = 200;
unset($third);
?>
Note: Trying to access or use a variable that's been unset( ), as in the preceding script,
will result in a PHP "undefined variable" error message. This message may or may not be
visible in the output page, depending on how your PHP error reporting level is
configured.
$name="Rish";
$NAME="rahul";
echo $name."<br/>";
echo $NAME;
?>
<?php
//define variables
$name = "Avah";
$age=25;
var_dump ($name);
var_dump($age);
?>
<?php
$first = 100;
$second = 200;
var_dump ($third);
?>
<?php
$first = 100.5;
$second = 200.2;
var_dump ($third);
?>
<?php
$bool = true;
var_dump ($bool);
?>
$name="Aron";
$name="Ainah";
echo $name."<br/>";
echo $name."<br/>";
echo $Aron;
?>
Output
Aron
Ainah
Ainah
In the above example $name is just a variable with string value="Aron". $$name is
reference variable.
$$name uses the value of the variable whose name is the value of $name.
echo $name print the value: Aron echo $$name print the value:Ainah \ value of
this($name) variable is act as reference of second variable($$name) .
echo $Aron print the value :Ainah \ Here $Aron is also act as reference variable.
Example - 2
<?php
$x = "100";
$x = 200;
echo $x."<br/>";
echo $x."<br/>";
echo "$100";
?>
Output
100
200
200
When you set $x to a value, it will replace that variable name with the value of the
variable you provide.
$$x(reference variable) hold value = 200. now we want to print the value.
echo $100 gives value.200. because it also act as a reference variable for value = 200.
Example 3.
<?php
$name="Aron";
${$name}="Ainah";
echo $name."<br/>";
echo ${$name}."<br/>";
echo "$Aron"."<br/>";
?>
Example 4.
<?php
$name="Randy";
${$name}="Ricky";
${${$name}}="Rish";
echo $name;
echo ${$name};
echo ${${$name}};
?>
Output
Randy
Ricky
Rish
variable ${$ {$name} } hold value ="Rish" // it act as "variable's of variable of variable"
reference.
It is used to collect value from a form(HTML script) sent with method='get'. information
sent from a form with the method='get' is visible to everyone(it display on the browser
URL bar).
2) $_POST["FormElementName"]
It is used to collect value in a form with method="post". Information sent from a form is
invisible to others.(can check on address bar)
3) $_REQUEST["FormElementName"]
This can be used to collect data with both post and get method.
4) $_FILES["FormElementName"]
$_FILES["FormElementName"]["ArrayIndex"]
: Such as File Name, File Type, File Size, File temporary name.
5) $_SESSION["VariableName"]
A session variable is used to store information about a single user, and are available to
all pages within one application.
6) $_COOKIE["VariableName"]
A cookie is used to identify a user. cookie is a small file that the server embedded on
user computer.
7) $_SERVER["ConstantName"]
Example
$_SERVER["SERVER_PORT"]
$_SERVER["SERVER_NAME"]
$_SERVER["REQUEST_URI"]
//third is blank
$z=0;
//again store $z in $y
$y=$z;
//Print the reversed Values
echo "<p align='center'>Now Fi rst numebr is : ". $x ."<br/>";
echo "and Second number is : ". $y."</p>";
}
?>
<form method="post">
<table align="center">
<tr>
<td>Enter First number</td>
<td><input type="text" name="fn"/></td>
</tr>
<tr>
<td>Enter Second number</td>
<td><input type="text" name="sn"/></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" value="Swap Numbers" name="swap"/></td>
</tr>
</table>
</form>
?>
Without Using Third Variable but using Some Predefined
Functions
<?php
$a = 10;
$b = 20;
?>
Constant in PHP
1. Constants are PHP container that remain constant and never change
2. Constants are used for data that is unchanged at multiple place within our program.
3. Variables are temporary storage while Constants are permanent.
4. Use Constants for values that remain fixed and referenced multiple times.
Syntax:
<?php
define('ConstName', 'value');
?>
Valid and Invalid Constant declaration:
<?php
define('SUM 2',ONE+TWO);
define('@SUM',ONE+TWO);
?>
define('NAME', "Rish");
?>
In the above example We define a constant using define( ) function. first argument for
name of constant and second for its value="PhilSCA". Now we print the value. Pass
name of constant inside print statement Output will become
define('ONE', 100);
define('TWO', 100);
define('SUM',ONE+TWO);
?>
<?php
define('X', 1000);
define('Y', 500);
define('Z',X - Y);
?>
In the above example We define three constant with name(X,Y,Z). First subtract the value
of two defined constant . Now result of these two value work as a value for third define
constant(Z). Pass Z inside print so it will show the subtraction of two value.
define('ONE', 100);
define('TWO', 100);
$res= ONE+TWO;
?>
In the above example We define two constant with name(one,two) and value(100,100)
respectively. $res variable is also define. Now we perform addition of two defined
constant value and result store in a variable($res = one+two;). To print the result pass
$res inside print statement.
<?php
?>
Output
if you have been following along, the script should be fairly easy to understand. It
begins by defining a constant named "EXCHANGE_RATE" which surprise, surprise stores
the dollar-to-euro exchange rate(assumed here at 1.00 USD to 0.80 EUR). Next, it
defines a variable named "$dollars" to hold the number of dollars to be converted, and
then it performs an arithmetic operation using the * operator, the "$dollars" variable,
and the "EXCHANGE_RATE" constant to return the equivalent number of euros. This
result is then stored in a new variable named "$euros" and printed to the Web page.
__LINE__
The current line number of the file.
<?php
?>
__FILE__
The full path and filename of the file.
<?php
?>
<?php
class demo
function test()
function testme()
$object=new demo();
$object->test();
$object->testme();
?>
Output Function of demo class : test Method of demo class : demo::testme Class :
demo
PHP_VERSION
The PHP version
<?php
?>
PHP_INT_MAX
The PHP integer value limit
<?php
?>
echo
1. echo is a statement i.e used to display the output. it can be used with parentheses echo
or without parentheses echo.
2. echo can pass multiple string separated as ( , )
3. echo doesn't return any value
4. echo is faster then print
For Example
<?php
$name="John";
echo $name;
//or
echo ($name);
?>
Output John
In the above example Create and initialize variable($name) hold a string value="John".
We want to print the name for this ($name) variable declare inside the echo with or
without parentheses . It will display the same output.
<?php
$name = "John";
$profile = "PHP Developer";
$age = 25;
echo $name , $profile , $age, " years old";
?>
In the above example $name , $profile and $age are three variable with value=("John" ,
"php developer" and 25) respectively. now we want to print all three variable values
together. all variables names are define inside the echo statement separated by comm
or dot(, or .) it will show the output
<?php
$name = "John";
$ret = echo $name;
?>
In the above example In this program we check the return type of "echo". declare a
variable $name with value="John". now we check the return type. when we run the
program it show error, because echo has no return type.
Print
1. Print is also a statement i.e used to display the output. it can be used with parentheses
print( ) or without parentheses print.
2. using print can doesn't pass multiple argument
3. print always return 1
4. it is slower than echo
For Example
<?php
$name="John";
print $name;
//or
print ($name);
?>
Output John
In the above example Declare a variable ($name) value="John". now we want to print
the name. we simply define $name inside print statement with or without parentheses. it
will show the output: "John" .
<?php
$name = "John";
$profile = "PHP Developer";
$age = 25;
print $name , $profile , $age, " years old";
?>
In the above example Declare three variable $name, $profile, $age and hold the
value("John","php developer",25). Now check whether it will allow execute multiple
argument. Pass three variable inside the print statement separated by comma. As we run
this program it show some error. It means multiple argument are not allow in print .
<?php
$name = "John";
$ret = print $name;
//To test it returns or not
echo $ret;
?>
Output John
In the above example declare a variable $name hold value="John".now we check the
return type of print . So (print $name )is store in a variable($ret) . it will show $name
value with return type=1.
In the given example user has to enter his/her name in text box, after entering the input
he/she has to click on submit button to display the name entered by him/her. You can
see the inputted value in address bar(url) also.
<head>
<?php
echo $_GET['n'];
?>
<title>get_browser</title></head>
<form method="GET">
<tr>
</tr>
<tr>
<td colspan="2" align="center">
</tr>
</table>
</form>
</body>
</html>
In the given above example: user entered the name inside the text box , after en tering
the name he clicked on submit button and can see the output of the program means
their name. User can check the input given by the user shows inside the URL because of
get method.
<head>
<title>get_browser</title>
<?php
error_reporting(1);
$x=$_GET['f'];
$y=$_GET['s'];
$z=$x+$y;
?>
</head>
<tr>
</tr>
<tr>
</tr>
<tr align="center">
</tr>
</table>
</form>
</body>
</html>
In the given above example: user has to enter the first number, second number after
given the input click on "+" button, and check the output means the sum of two
numbers. can also see the input given by him/her displays on address-bar(URL).
<head>
<?php
echo $_POST['n'];
?>
<title>get_browser</title>
</head>
<form method="post">
<tr>
</tr>
<tr>
</tr>
</table>
</form>
</body>
</html>
In the given above example: user enters the name inside text box, after en tered the
name inside text box click on submit button it will display the name entered by user like
user enters "PhilSCA" inside the text box the output displays "PhilSCA". In this example
we have used Form POST method. So the user's input doesn't display on address-bar.
<head>
<title>get_browser</title>
<?php
error_reporting(1);
$x = $_POST['f'];
$y = $_POST['s'];
$z = $x + $y;
?>
</head>
<tr>
</tr>
<tr>
</tr>
<tr align="center">
</tr>
</table>
</form>
</body>
</html>
In the given above example: user enters the first number inside first text box and second
number inside second text box, after entered the value inside the text box, clicked on
"+" button. The program displays the output Sum = addition of two numbers.
error_reporting(1);
$id = $_POST['id'];
$pass = $_POST['pass'];
if(isset($_POST['signin']))
header('location:https://www.philsca.edu.ph');
else
{
echo "<font color='red'>Invalid id or password</font>";
?>
<body>
<form method="post">
<tr>
</td>
</tr>
<tr>
</td>
</tr>
<tr>
</td>
</tr>
</table">
</form>
</body>
In the given above example: there is a secure login page. in which user enters the valid
user_name and password, after entering the valid user_name and password he has to
clicked on Sign-In button. authorized user can visit next page and for unauthorized u ser
it shows an error message.
<body>
<tr>
</tr>
<tr>
</td>
</tr>
</table>
</form>
</body>
PHP Script
Save it as Logic.php
<?php
$name=$_POST['n'];
?>
First we make Form using HTML script. We design a textbox to take input through user
and a submit button with value("show my name") . When name is entered by user and
click on submit button the value of textbox redirect to php script page. Because Action
Attribute is used here for link . Information that is send by user is collect by using
$_POST[] and store in a local variable($name). Now a local variable is concatena te with
String(“welcome”) and print, output will become Welcome Ainah.
Write a program to add two numbers and print the result in third text box.
To display the output in third text box there are many ways. First way : Take input from
HTML make calculation and display the output in text box, for this use <input
type="text" value="output"/> inside PHP script. like:
<?php
if(isset($_POST['add']))
{
$x=$_POST['fnum'];
$y=$_POST['snum'];
$sum=$x+$y;
echo "Result:<input type='text' value='$sum'/>";
}
?>
<body>
<form method="post">
Enter first number <input type="text" name="fnum"/><hr/>
Enter second number <input type="text" name="snum"/><hr/>
<input type="submit" name="add" value="ADD"/>
</form>
</body>
Output
2000
Result:
1000
Enter first number
1000
Enter second number
ADD
First we design two textbox using HTML script with attribute name with ( value="Fnum"
for first textbox) and (value= "Snum" for second textbox). A submit button with
(name=Add).
When we run the program the logic that is defined inside PHP script, $_POST[ ] is used
to collect the values from a form. it store the value in variables ($x,$y).
But we want to show the sum inside the textbox. for this we define textbox inside the
"echo" statement with (value="$sum").
SWAP Sum of two number and display the Result in third text
box
<?php
$x=$_POST['fnum'];
$y=$_POST['snum'];
$sum=$x+$y;
?>
<body>
<form method="post">
Result <input type="text" value="<?php echo @$sum;?>"/><hr/>
Enter first number <input type="text" name="fnum"/><hr/>
Enter second number <input type="text" name="snum"/><hr/>
<input type="submit" value="ADD"/>
</form>
</body>
Output
2000
Result:
1000
Enter first number
1000
Enter second number
ADD
In previous example,
the textbox is defined inside the PHP script. Instead this We define third textbox outside
the PHP script.
Value attribute of <Input> tag is used to show the output inside the textbox here we
define a PHP script inside the value Property of the textbox.
<html>
<body>
<form method="post" action="output.php">
<table bgcolor="#C4C4C4" align="center" width="380" border="0">
<tr>
<td align="center"colspan="2"><font color="#0000FF" size="5">Registration
Form</font></td>
</tr>
<tr>
<td width="312"></td>
<td width="172"> </td>
</tr>
<tr>
<td><Enter Your Name </td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Enter Your Email </td>
<td><input type="email" name="email" /></td>
</tr>
<tr>
<td>Enter Your Password </td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td>Enter Your Mobile Number </td>
<td><input type="number" name="num" /></td>
</tr>
<tr>
<td>Enter Your Address </td>
<td><textarea name="address"></textarea></td>
</tr>
<td align="center" colspan="2"><input type="submit" value="save" name="submit"
/></td>
</table>
</form>
</body>
</html>
Output
Registration Form
Enter Your Name phptpoint
save
Output.php
Output
Your Email is
Your Address is
First for name,Second for Email_id, Third for Password,Fourth for Mobile No,and Fifth for
address. A button is used to display data on next page after click on this button.
inside the (output.php) page we create same form format. But value attribute of every
<input> type tag is used to declare the PHP script inside the value with $_POST[ ] .
extract($_POST);
?>
<html>
<head>
</head>
<body>
<form method="post">
<Tr>
<th>Result</th>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
</tr>
</table>
</form>
</body>
</html>
Add two textbox values and display the sum in third
textbox using extract method
Enter two numbers and display the output in third text box when u click on submit button. Keep
in mind one thing First and second text values doesn't reset. Result text box should not writable .
PHP Script
<?php
extract($_POST);
//do addition and store the result in $res
if(isset($add))
{
$res=$fnum+$snum;
}
?>
HTML Form
<html>
<head>
<title>Display the result in 3rd text box</title>
</head>
<body>
<form method="post">
<table align="center" border="1">
<Tr>
<th>Your Result</th>
<td><input type="text" readonly="readonly" value="<?php echo
@$res;?>"/></td>
</tr>
<tr>
<th>Enter first number</th>
<td><input type="text" name="fnum" value="<?php echo
@$fnum;?>"/></td>
</tr>
<tr>
<th>Enter second number</th>
<td><input type="text" name="snum" value="<?php echo
@$snum;?>"/></td>
</tr>
<tr>
<td align="center" colspan="2">
<input type="submit" value="+" name="add"/>
</tr>
</table>
</form>
</body>
</html>
Operatos Description
+ Add
- Subtract
* Multiply
$x=10;
$y=5;
//addition
$sum=$x+$y;
echo "sum=".$sum."<br/>";
?>
Output sum = 15
$x=10;
$y=5;
//subtraction
$sub=$x-$y;
echo "sub=".$sub."<br/>";
?>
Output sub = 5
$x=10;
$y=5;
//Multiply
$multiply=$x*$y;
?>
Output Multiplication = 50
Arithmetic operators (/) for quotient
<?php
$x=10;
$y=5;
//quotient
$div=$x/$y;
?>
Output Div = 2
$x=10;
$y=3;
//remainder
$rem=$x%$y;
echo "remainder=".$rem."<br/>";
?>
Output sub = 0
$x and $y are two integer variables here there are five blocks/modules in this example
they are to preform addition, subtraction, multiplication, division and modulus
respectively.
$x store the value 10, $y store the value 5. The output of first module is addition of two
values 10 and 5 that is 15 ($x+$y=15).
The output of second module is subtraction of two values 10 and 5 that is 5 ($x-$y=5).
The output of third module is multiplication of two values 10 and 5 that is 50
($x*$y=50).
The output of fourth module is division of two values 10 and 5 that is 2 ($x/$y=2).
The output of last module is modulus of two values 10 and 3 that is 1 ($x%$y=1).
Operatos Description
$x = 500;
$x+= 500;
echo "sum=".$x."<br/>";
?>
Output sum=1000
$x = 1000;
$x-= 500;
?>
$x = 100;
$x*= 10;
?>
$x = 1000;
$x/= 500;
Output Quotient = 2
$x = 5;
$x%= 2;
?>
Output Remainder= 1
In the above example. Variable($x) with value =5. Now calculate the modulus using ($x%=2) .
it gives remainder value="1" and this remainder assign again to variable($x).
and the output will become : 1
Concatenate and assign
<?php
echo $str."<br/>";
?>
Operatos Description
== Equal to
!= Not equal to
<?php
$x=10;
$y=10.0;
echo ($x==$y);
//it returns true because both the variable contains same value.
echo ($x===$y);
/*it returns false because === strongly compares.
here both variable contain same value i.e 10 but different datatype one is
integer and another is float.*/
?>
in the above example. Two variable $x , $y define $x hold the value 10 $y hold value 10.0 Now
perform several operation on this First check ($x==$y)=>it returns true because the value for
both is same Second Check($x===$y)=>it returns false because now it also compare data-type.
$y hold a float value.
//another example
$bool=(boolean)1;
$int=(integer)1;
//return false because both have same value but diff data type
echo ($bool===$int);
?>
$bool=(boolean)1 ($bool==$int) it returns true because both have same value $int= (integer)1
($bool===$int) its return false because both have different data type
$a=10;
$b=11;
echo $a>$b;
//return false because $a is less than $b.
echo $a<$b;
//return true because $a is less than $b.
echo $a>=$b;
//return false because neighter $a is greater nor equal to $b
echo $a<=$b;
//return true because $a is than $b.
?>
$a hold the value 10 $b hold the value 11 check ($a>$b)=> returns false because $a less
than $b. check($a>=$b)=> returns true because $a neither grater nor equal to $b.
Logical Operators
Operatos Description
&& and
|| or
! not
AND(&&) Operator
Operator name and pass
Description If name and pass both are true then result true.
Explanation if name = = "alex" and pass = = "alex123" then it will redirect on PhilSCA page, and if any
one of these is not valid then it shows an error message(Invalid name or password).
Eg of and operator
<?php
$name="alex";
$pass="alex123";
header('location:https://www.philsca.edu.ph');
else
?>
In the above example Two variable $name and $pass with value("alex","alex123") If both
value are exist then it will redirect on PhilSCA.com because of header( ). Otherwise
invalid name or password. Here both the condition are true so as output you will be
redirected on "https://www.philsca.edu.ph" page.
OR(||) Operator
Operator name or pass
Explanation if name = = "alex" or pass = = "alex123" then it will redirect on PhilSCA page, and if both are
false then it shows an error message(Invalid name or password).
Eg
<?php
$name="alex";
$pass="alex123";
if($name=="alex" || $pass=="alex12345")
header('location:https://www.philsca.edu.ph');
else
?>
in the above example Two variable $name or $pass $name hold the value="alex" $pa ss
hold the value="alex123" if any of one condition is true then redirect you on
"https://www.philsca.edu.ph" page otherwise so invalid name or password. Here one of
the both condition is true so as output you will be redirected on
"https://www.philsca.edu.ph" page.
Not(!) Operator
Operator not
Explanation check given number is odd or not. Here $num stores 11 and its modulus is 1 . By example
$num mudulus is not equal to 0 it is an odd number so 11 is an odd numder.
Eg
<?php
$num=11;
if($num%2!=0)
else
?>
In the above example take a variable $num with value = 11 let we check number is odd
or even. we give a condition($num%2!=0) inside if it will not true then number is odd.
otherwise else statement is execute ( Number is even ). Here number is not divided by 2
so the output display : given number is odd number
$eid=$_GET['e'];
$pass=$_GET['p'];
if($eid=="" || $pass=="")
else
else
?>
<form>
</form>
Output
wrong email or pass
sanjeev
Enter your email
******
Enter your pass
Signin
In the above example Here, we create a form of two field. By default, the method of
form is 'GET' First filed is for Email Second filed is for password A logic is define in PHP
script. First it's check the isset( ) function for existence, Enter the name and password in
name and password field it store value in variables ($eid and $pass). if either $eid or
$pass value is null then a message is shown "fill your email or password". Otherwise it
checks the $eid and $Pass value with the given existing value. if match then message
show "welcome xyz" else message show "wrong email or password."
PHP's precedence rules are tough to remember. Parentheses always have the highest
precedence, so wrapping an expression in these will force PHP to evaluate it first, when
using multiple sets of parentheses.
<?php
echo (((4*8)-2)/10);
echo (4*8-2/10);
?>