0% found this document useful (0 votes)
14 views42 pages

PHP

php

Uploaded by

davalem108
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views42 pages

PHP

php

Uploaded by

davalem108
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

PHP

INTRODUCTION
It world’s most popular programming languages for web
development, the PHP hypertext preprocessor
Today in use on over twenty million Web sites and more than a third
of the world’s Web servers.
Version Release Date
1.0 8 June 1995
2.0 1 November 1997
3.0 6 June 1998
4.0 22 May 2000
5.0 13 July 2004
6.x cancel
7 3 December 2015(stable)
7.3 6 December 2018(current
version)
Advantage of PHP
Performance
Portability(platform independent)
Ease of use
Open source
Third-party application support
Community support
Applications of PHP
PHP performs system functions, i.e. From files on a system it can
create, open, read, write, and close them.
PHP can handle forms, i.e. Gather data from files, save data to a file,
through email you can send data, return data to the user.
You add, delete, modify elements within your database through php.
Access cookies variables and set cookies.
Using PHP, you can restrict users to access some pages of your
website.
It can encrypt data.
PHP SOFTWARE REQUIREMENT
1. PHP server
The PHP community provides some types of software server
solution under the GNU (general public license).
These are the following:
Server Stands for
WAMP server
WAMP Microsoft window o/s, Apache
LAMP server Mysql PHP
LAMP Linux Operating System Apache
MAMP server Mysql PHP
MAMP Mac os Apache Mysql PHP
XAMPP server
XAMPP x-os(cross operating system)
Apache Mysql PHP Perl
PHP XAMPP installation
Download the file from https://
www.apachefriends.org/download.html and install as ordinary
software
Open the application called Xampp
Save file as following... Create a directory inside
xampp/htdocs/myproject/
Save it inside: c:xampp/htdocs/myproject/firstprog.Php
Run the PHP script
Open your browser and write in url localhost/myproject/firstprog.Php
PHP syntax
Syntax is a way to representation of PHP script.
Basically it gives the primary idea to specify the code format.
It also specify the area of a written code.
There are three ways to start PHP environment
1. The most commonly and effective PHP syntax:
<?php echo "Welcome to the world of php"; ?>

In the given example it begin with (< ?Php) and end with(? >).
Echo statement is used to print the given string.
Mandatory closing in("abcd")double quotes.
Terminate the script with semicolon because semicolon is known as terminator.
PHP syntax
As mentioned earlier, PHP is embedded in HTML.
To run PHP script you have to start your server like apache
Save with file extension .php

<html>
<head>
<title>Hello World</title>
</head>
<body>
<?php
echo "Hello, World!";
?>
</body>
</html>
CONT.
2. Short or short-open tags look like this:
<? echo "welcome to the world of php"; ?>

We use short tags.


Start and end with < > respectively.
Declare the statement in between < >, to display the output on
browser short open tags smoothly works on xampp server but face
problem on wamp server if you are using wamp server first you
have to set the short_open_tag setting in your php.Ini file to on
CONT.
3. HTML script tags:
<script language="PHP"> echo "welcome to the world of php"; </script>

PHP is the server side scripting language.


So HTML script is also used to start PHP environment like other
scripting language.
PHP COMMENTS
A comment is non-executable lines.
Comment is used to write description for your own understanding.
Browser doesn't read the comments.
There are two types of comments used in php
1. Single line comments :
Single line comment used for short explanations.
Declaration of single line comment are two types
Either begin with(#) Or backslash(//)
CONT.
<?php
# This is the single line comment
# This is the next line comment
// This is also a single line comment.
?>
In the above example.
*First and second line comments begin with hash(#).
*The third one is begin with(//).
If we check the output of the given example.
Browser show blank page.
Because comments are always non-executable..
2. Multi-lines comments :
Multi lines comments used to comment multiple lines.
Here we can give comments in bulk the bulk comments are enclose
<?php
within (/*.....*/)
/*
This is a comment with multiline
Developer : Abebe
view : Multiline Comments Demo
*/
?>
 The all lines which is define in php evironment are Multiline comments. it
is non-executable.
 Because it enlose with Multiline comments statement.
PHP VARIABLES
Variable is nothing it is just name of the memory location.
A variable is simply a container i.e used to store both numeric and
non-numeric information.
Rules for variable declaration
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 _ )
A variable name should not contain space
Assigning values to variables
Assigning a value to a variable in PHP is quite east: use the
equality(=) symbol, which also to the php's assignment operators.
This assign value on the right side of the equation to the variable on
the left.
<?php
$myCar = "Honda";
echo $myCar;
?>

A variable is created the moment you assign a value to it:


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
<?php
$myCar = "Honda City";
echo $myCar." is riding"; ?>

In the above example Variable($mycar) hold value="honda city". Now we


wants to concatenate variable with string. pass this variable($mycar) inside
echo statement. To concatenate this with a string("is riding") use dot(.)
between variable name and string. The output will be displayed : Honda
City is riding
TRY

<?php
$first = 100;
$second = 200;
$third = $first + $second;
echo "Sum = ".$third; ?>
 In the above example Declare $first , $second variable with value=100,
200 respectively.
 Now add these two numbers using arithmetic operator ("+"). sum of these
two variable result stored in a third variable($sum).
 Now print the sum passing ($third) with echo statement with a string.
<?php
$first = 1000;
$second = 500;
$third = $first - $second;
echo "Subtraction = ".$third;
?>
 In the above example We perform subtraction using
variables( $first, $second) with vale=1000,500.
 Subtract second variable from first, result is hold by
third variable($third) .
 Print this third variable passing with echo statement.
Destroying PHP variables
To destroy a variable, pass the variable to php's unset( ) function. As in the following
<?php
example:
$name="steve";
echo $name; //unset( ) function destroy the variable
reference. unset($name);
?>
Output Sum = 300
Sum = Notice error undefined third variable
 In the above example declare variable $name hold value="steve".
 In this program we used unset() function to delete a particular variable.
 First it show 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).
Variable names in PHP are case-sensitive
<?php
$name="rexx";
$NAME="rahul";
echo $name."<br/>";
echo $NAME; ?>

Output rexx rahul


 Variable names in PHP are case-sensitive.
 As a result, $name refers to a different variable than does $NAME.
Inspecting Variable Contents(Variable Property)
 PHP offers the var_dump( ) function, which accepts a variable and X-rays it
for you. Here's an example <?php
//define variables
$name = "Fiona";
$age=25; //display variable contents Output string 'Fiona' (length=5) int 25
var_dump ($name);
var_dump($age);
?>
In the above example we use var_dump( ) function to check the contents(property) of variable,
$name hold a string value ="fiona" while $age hold an integer value = 25. Now pass this variable
inside var_dump($name,$age) function . it show all information related this(data type of
variable, length(only string), value)
<?php <?php
$first = 100; $first = 100.5;
$second = 200.2;
$second = 200; $third = $first + $second;
$third = $first + $second; var_dump ($third);
var_dump ($third); ?>
?> Output float 300.7
Output int 300 <?php
$bool = true;
var_dump ($bool); Output Boolean true
?>
Difference between $var and $$var in PHP
PHP $$var uses the value of the variable whose name is the value of $var.
It means $$var is known as reference variable where as $var is normal variable.
It allows you to have a "variable's variable" - the program can create the variable
name the same way it can create any other string.
EXCERCISE
1. Write a program to print “hello world” using echo
<?Php
echo "hello world";
?>
2. Write a program to print “Hello PHP” using variable
<?php
$message = "Hello PHP";
echo $message;
?>
3. Write a program to print a string using echo+variable.
<?php
$message = "Welcome to the PHP World";
echo $message;
?>
4. Write a program to print two variables in single echo
<?php
$message_1 = "Good Morning.";
$message_2 = "Have a nice day!";
echo $message_1." ". $message_2;
?>
5. Write a program to count 5 to 15 using PHP loop
 Rules & hint
 You can use “for” or “while” loop
 You can use variable to initialize count
 You can use html tag for line break

<?php
$count = 5;
while($count <= 15)
{
echo $count;
echo "<br>" ;
$count++;
}
?>
ARITHMETIC OPERATORS
 There are following arithmetic operators supported by PHP language
 Assume variable A holds 10 and variable B holds 20 then

Operator Description Example


+ Adds two operands A + B will give 30

- Subtracts second operand from the first A - B will give -10

* Multiply both operands A * B will give 200

/ Divide numerator by de-numerator B / A will give 2

% Modulus Operator and remainder of after an B % A will give 0


integer division

++ Increment operator, increases integer value A++ will give 11


by one
-- Decrement operator, decreases integer value A-- will give 9
by one
Comparison operators
There are following comparison operators supported by PHP language
Assume variable A holds 10 and variable B holds 20 then −
Operator Description Example
== Checks if the value of two operands are equal or not, if yes then condition becomes true. (A == B) is true.

!= Checks if the value of two operands are equal or not, if values are not equal then condition (A != B) is not true.
becomes true.

> Checks if the value of left operand is greater than the value of right operand, if yes then (A > B) is not true.
condition becomes true.

< Checks if the value of left operand is less than the value of right operand, if yes then (A < B) is true.
condition becomes true.
>= Checks if the value of left operand is greater than or equal to the value of right operand, if (A >= B) is not true.
yes then condition becomes true.

<= Checks if the value of left operand is less than or equal to the value of right operand, if (A <= B) is true.
yes then condition becomes true.
Logical operators
There are following logical operators supported by PHP language
Assume variable A holds 10 and variable B holds 20 then
Operator Description Example
and Called Logical AND operator. If both the operands are true then (A and B) is true.
condition becomes true.
or Called Logical OR Operator. If any of the two operands are non (A or B) is true.
zero then condition becomes true.
&& Called Logical AND operator. If both the operands are non zero (A && B) is true.
then condition becomes true.
|| Called Logical OR Operator. If any of the two operands are non (A || B) is true.
zero then condition becomes true.
! Called Logical NOT Operator. Use to reverses the logical state of !(A && B) is false.
its operand. If a condition is true then Logical NOT operator will
make false.
ASSIGNMENT OPERATORS
There are following assignment operators supported by PHP language

Operator Description Example


= Simple assignment operator, Assigns values from right side operands to left C = A + B will assign value of A
side operand + B into C
+= Add AND assignment operator, It adds right operand to the left operand and C += A is equivalent to C = C +
assign the result to left operand A
-= Subtract AND assignment operator, It subtracts right operand from the left C -= A is equivalent to C = C - A
operand and assign the result to left operand
*= Multiply AND assignment operator, It multiplies right operand with the left C *= A is equivalent to C = C *
operand and assign the result to left operand A
/= Divide AND assignment operator, It divides left operand with the right C /= A is equivalent to C = C / A
operand and assign the result to left operand
%= Modulus AND assignment operator, It takes modulus using two operands C %= A is equivalent to C = C
and assign the result to left operand %A
PHP - Decision Making
The if, elseif ...Else and switch statements are used to take decision based on the different
condition.
You can use conditional statements in your code to make your decisions. PHP supports following
three decision making statements −
If...Else statement − use this statement if you want to execute a set of code when a condition is
true and another if the condition is not true
Elseif statement − is used with the if...Else statement to execute a set of code if one of the
several condition is true
Switch statement − is used if you want to select one of many blocks of code to be executed, use
the switch statement. The switch statement is used to avoid long blocks of if..Elseif..Else code.
The if...Else statement
if you want to execute some code if a condition is true and another
code if a condition is false, use the if....else statement.
Syntax
if (condition) {
code to be executed if condition is true; }
else
{code to be executed if condition is false;}
Example
The following example will output "have a nice weekend!" If the
current day is Sunday, otherwise, it will output "have a nice day!":
<Html>

<body>

<?Php

$d = date("d");

if ($d == "fri")

echo "have a nice weekend!";

Else echo "have a nice day!";

?>

</Body>

</html>
The elseif statement
If you want to execute some code if one of the several conditions are
true use the elseif statement
Syntax
If (condition)
{code to be executed if condition is true; }
elseif (condition)
{code to be executed if condition is true; }
else
{code to be executed if condition is false;}
Example
The following example will output "have a nice weekend!" If the current day is friday, and "have a
nice sunday!" If the current day is sunday. Otherwise, it will output "have a nice day!"
<html>
<body>
<?php
$d = date("D");
if ($d == "Fri")
echo "Have a nice weekend!";
elseif ($d == "Sun")
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>
</body>
</html>
The switch statement
If you want to select one of many blocks of code to be executed, use the switch statement.
The switch statement is used to avoid long blocks of if..Elseif..Else code.
Syntax
switch (expression){
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed if expression is different from both label1 and label2; }

You might also like