Chapter 4 PHP
Chapter 4 PHP
Introduction to PHP
(Hypertext Preprocessor)
1 Prepared by: Melkamu D. 05/08/2024
What is PHP?
PHP stands for Hypertext Preprocessor
PHP is a powerful server-side scripting language for
creating dynamic and interactive websites.
PHP is perfectly appropriate for Web development
and can be embedded directly into the HTML
code.
The PHP syntax is very similar to C.
PHP is often used together with Apache (web
server) on various operating systems.
A PHP file may contain text, HTML tags and scripts.
2 Prepared by: Melkamu D. 05/08/2024
PHP cont.…
Scripts in a PHP file are executed on the server.
PHP files are returned to the browser as plain
HTML
PHP files have a file extension of ".php",
".php3", or ".phtml“
PHP is whitespace insensitive:
That means it almost never matters how many
whitespace characters you have in a row
including tabs and carriage returns (end-of-line
characters).
PHP is case sensitive:
3 Prepared by: Melkamu D. 05/08/2024
Cont.….
PHP supports many databases (MySQL, Informix,
Oracle, Sybase, Solid, PostgreSQL, Generic ODBC,
etc.).
PHP is an open source software (OSS).
PHP is free to download and use.
PHP runs on different platforms (Windows, Linux,
Unix, etc.).
PHP is compatible with almost all servers used today
(Apache, IIS, etc.).
PHP is easy to learn and runs efficiently on the server
side.
== Checks if the value of two operands are equal or not, if (A == B) is not true.
yes then condition becomes true.
> Checks if the value of left operand is greater than the (A > B) is not true.
value of right operand, if yes then condition becomes true.
< Checks if the value of left operand is less than the value (A < B) is true.
of right operand, if yes then condition becomes true.
>= Checks if the value of left operand is greater than or (A >= B) is not true.
equal to the value of right operand, if yes then condition
becomes true.
<= Checks if the value of left operand is less than or equal to (A <= B) is true.
the value of right operand, if yes then condition becomes
true.
19 Prepared by: Melkamu D. 05/08/2024
Logical Operators
Operator Description Example x=6, y=3
45
Prepared by: Melkamu D. 05/08/2024
Numeric Array
These arrays can store numbers, strings and any object but their
index will be represented by numbers.
By default array index starts from zero.
Use array() function to create array.
Example: showing how to create and access numeric arrays.
<html>
<body>
<?php This will produce following
result: Value is 1
/* First method to create array. */
Value is 2
$numbers = array( 1, 2, 3, 4, 5); Value is 3
foreach( $numbers as $value ) Value is 4
{ Value is 5
echo "Value is $value <br />";
}
?>
</body>
46 Prepared
</html> by: Melkamu D. 05/08/2024
Numeric Array…
The second method is to create an array.
<html>
<body>
<?php
$numbers[0] = "one";
$numbers[1] = "two"; This will produce following
$numbers[2] = "three"; result: Value is one
Value is two
$numbers[3] = "four"; Value is three
$numbers[4] = "five"; Value is four
foreach( $numbers as $value ) Value is five
{
echo "Value is $value <br />";
}
?>
</body> </html>
47 Prepared by: Melkamu D. 05/08/2024
Associative Arrays
The associative arrays are very similar to numeric
arrays in terms of functionality but they are
different in terms of their index.
An associative array will have its index as a
string so that you can establish an association
between keys and values.
For example, store the salaries of employees in an
array, you can use the employees’ name as the keys and
the value would be their respective salary.
NOTE: Don't keep an associative array inside the double quote
while printing otherwise it will not return any value.
48 Prepared by: Melkamu D. 05/08/2024
Example:
<html>
<body>
<?php
/* First method to create associate array. */
$salaries = array(
"mola" => 2000, Output
Salary of mola is 2000
“kindu" => 1000, Salary of kindu is 1000
"zeritu" => 500 Salary of zeritu is 500
);
echo "Salary of mola is ". $salaries['mola'] . "<br />";
echo "Salary of kindu is ". $salaries[‘kindu']. "<br />";
echo "Salary of zeritu is ". $salaries['zeritu']. "<br />";
?> </body> </html>
49 Prepared by: Melkamu D. 05/08/2024
Second method to create an associative array
<html>
<body> Output
Salary of mola is high
<?php Salary of kindu is medium
$salaries['mola'] = "high"; Salary of zeritu is low
$salaries[‘kindu'] = "medium";
$salaries['zeritu'] = "low";
echo "Salary of mola is ". $salaries['mola'] .
"<br />";
echo "Salary of kindu is ". $salaries[‘kindu']. "<br />";
echo "Salary of zeritu is ". $salaries['zeritu']. "<br />";
?>
</body>
</html>
50 Prepared by: Melkamu D. 05/08/2024
Multidimensional Arrays
In multi-dimensional array each element in the main
array can also be an array.
And each element in the sub-array can be an array, and so
on.
Values in the multi-dimensional array are accessed using
multiple index.
Example
In this example we create a two dimensional array to
store marks of three students in three subjects:
This example is an associative array, you can create
numeric array in the same fashion.
51 Prepared by: Melkamu D. 05/08/2024
Example:
<html> )
<body> );
<?php /* Accessing multi-dimensional array
$marks = array( values */
“Abe" => array ( echo "Marks for Abe in physics : " ;
"physics" => 35, echo $marks[‘Abe']['physics'] .
"maths" => 30, "<br />";
"chemistry" => 39 echo "Marks for kasu in maths : ";
), echo $marks[‘kasu']['maths'] .
“kasu" => array ( "<br />";
"physics" => 30, echo "Marks for zena in chemistry : "
"maths" => 32, ;
"chemistry" => 29 echo $marks['zena']['chemistry'] .
), "<br />";
"zena" => array ( ?>
"physics" => 31, </body>
"maths" => 22, </html>
52 "chemistry" => 39
Prepared by: Melkamu D. 05/08/2024
String Operation
String Concatenation Operator
To concatenate two string variables together, use the
dot (.) operator:
<?php
$string1="Hello World";
$string2="1234";
echo $string1 . " " . $string2;
?>
This will produce following result:
Hello World 1234
55
string is 0, and not 1.
Prepared by: Melkamu D. 05/08/2024
PHP Functions
A function is a block of code that can be executed whenever
we need it.
Creating PHP functions:
All functions start with the word "function()“.
Name the function - It should be possible to understand what
the function does by its name.
The name can start with a letter or underscore (not a
number).
Add a()"{" The function code starts after the opening curly
brace.
Insert the function code
Add a() "}" The function is finished by a closing curly brace.
56 Prepared by: Melkamu D. 05/08/2024
Function Example
<html>
<head>
<title>Writing PHP Function</title>
</head>
<body>
<?php
/* Defining a PHP Function */
function writeMessage()
{
echo "You are really a nice person, Have a nice time!";
}
/* Calling a PHP Function */
writeMessage();
57
?>Prepared by: Melkamu D. 05/08/2024
</body> </html>
Function Example…
A simple function that writes a name when it is called:
<html>
<body>
<?php
function writeMyName()
{
echo “Melkamu D.";
}
writeMyName();
?>
</body>
</html>
When the user clicks the "Submit" button, the URL will
not contain any form data, and will look something like
this:
localhost/welcome.php
! !!
OU
Y
N K
A
TH