Professional Documents
Culture Documents
Chapter 5 IP
Chapter 5 IP
CHAPTER FIVE
Basics of PHP
Mulugeta G..
2
Introduction
• PHP stands for Hypertext Preprocessor
operating systems.
Introduction
• PHP files are returned to the browser as plain HTML
• PHP can create, open, read, write, delete, and close files on the server
• You can output images, PDF files, and even Flash movies.
• You can also output any text, such as XHTML and XML.
5
• Syntax:
<?php
?>
Output statements
• Two basic statements to output text with PHP: echo and print.
Echo Format Print Format
echo output1, output2, output3, output4 print output;
?>
7
Comments
• Is a line that is not read/executed as part of the program.
<?php
// This is a single-line comment
/*
This is a
multiple line comment
block in Php
*/
?>
8
Variables
• Variables are used for storing values, such as numbers, strings or function
results.
• $var_name = value;
Variables
• PHP automatically converts the variable to the correct data type,
Data types
• Boolean (bool or boolean)
• Simplest of all
• String (string)
• Hold strings of characters within either single quote (‘) or double quote (‘’)
Data types
• Array
• NULL
Strings
• Strings: are sequences of characters within single or double quotes
<?php
$variable = "name";
$literally = 'My $variable will not print!';
print($literally);
$literally = "My $variable will print!\n";
print($literally);
?>
• This will produce following result:
My $variable will not print!
My name will print
• There are no limits on string length
13
Constants
• A constant is a name or an identifier for a simple value.
undefined.
14
Constants
• To define a constant you have to use define() function
Operator Types
• What is Operator?
Control Statements
• You can use conditional statements in your code to make your
Example
<?php <?php
$t = date("H"); $t = date("H");
if ($t < "20") { echo "<p>The hour (of the server)
echo "Have a good day!"; is " . $t;
} echo ", and will give the following
?> message:</p>";
_______________________
<?php if ($t < "10") {
$t = date("H");
echo "Have a good morning!";
if ($t < "20") { } elseif ($t < "20") {
echo "Have a good day!"; echo "Have a good day!";
} else { } else {
echo "Have a good night!"; echo "Have a good night!";
}
?> }
?>
18
Example
<?php <html>
$favcolor = "red"; <body>
switch ($favcolor) {
case "red": <?php
echo "Your favorite color is function myTest() {
red!"; static $x = 0;
break; echo $x;
case "blue":
echo "Your favorite color is $x++;
blue!"; }
break; myTest();
case "green": echo "<br>";
echo "Your favorite color is myTest();
green!";
break; echo "<br>";
default: myTest();
echo "Your favorite color is ?>
neither red, blue, nor green!";
} </body>
?>
</html>
19
Control Statements
• Instead of adding several almost equal code-lines in a script, we can use
true
• do...while - loops through a block of code once, and then repeats the loop as
Example
<?php <?php
$x = 1; for ($x = 0; $x <= 10; $x++)
{
while($x <= 5) { echo "The number is: $x <br>";
echo "The number is: $x <br>"; }
$x++; ?>
}
?> ________________________
____________________________ <?php
<?php $colors = array("red", "green",
$x = 1; "blue", "yellow");
do { foreach ($colors as $value) {
echo "The number is: $x <br>"; echo "$value <br>";
$x++; }
} while ($x <= 5);
?> ?>
21
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:
<?php
?>
<?php
echo strpos("Hello world!", "world"); // outputs 6
?>
• Replace Text Within a String ------ str_replace()
<?php
echo str_replace("world", "Dolly", "Hello world!"); // outputs Hello Dolly!
?>
25
THANK YOU
Part II
26
PHP Functions
• is a block of code that can be executed whenever we need it.
• Syntax:
function function_Name()
{
// block of code
}
27
Example
A simple function that writes a name when it is called:
<html>
<body>
<?php
function writeMyName()
{
echo “abebe belachewu";
}
writeMyName();
?>
</body>
</html>
28
• Example: two integer parameters and add them together and then print them.
<?php
function addFunction($num1, $num2)
{
$sum = $num1 + $num2;
echo "Sum of the two numbers is : $sum";
}
addFunction(10, 20);
?>
29
Example
<html>
<body>
<?php
function writeMyName($fname)
{
echo “$fname <br />";
}
echo "My name is ";
writeMyName(“abebe");
?>
</body>
</html>
30
• The most important thing to notice when dealing with HTML forms
and PHP is that any form element in HTML page will automatically
be available to your PHP scripts.
Form example:
<form action="welcome.php" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
31
PHP $_GET
• The $_GET variable is used to collect values from a form with
method="get".
• The $_GET variable is an array of variable names and values
are sent by the HTTP GET method.
Example
<form action="welcome.php" method="get">
</form>
33
PHP $_GET
• When the user clicks the "Submit" button, the URL
sent could look something like this:
localhost/welcome.php?name=Peter&age=37
• The "welcome.php" file can now use the $_GET variable to
catch the form data
• Note that the names of the form fields will automatically be the
ID keys in the $_GET array:
Welcome <?php echo $_GET["name"]; ?>.<br />
PHP $_POST
• The $_POST variable is used to collect values from a form
with method="post".
Example
<form action="welcome.php" method="post">
Enter your name: <input type="text" name="name" />
Enter your age: <input type="text" name="age" />
<input type="submit" />
</form>
• When the user clicks the "Submit" button, the URL will not
contain any form data, and will look something like this:
localhost/welcome.php
37
from form data sent with both the GET and POST methods.
• Example
• The two functions are identical in every way, except how they
handle errors:
execution
• When the header needs to be updated, you can only update the
include file, or when you add a new page to your site, you can
simply change the menu file (instead of updating the links on all
your web pages).
40
errors differently.
• The require() generates a fatal error, and the script will stop.
PHP Cookies
• A cookie is used to identify a user.
computer.
PHP Sessions
• A session is a way to store information (in variables) to be used across
multiple pages.
• Session variables hold information about one single user, and are
• Session variables are set with the PHP global variable: $_SESSION.
44
PHP MySQL
• MySQL is the most popular open-source database system.
Parameter Description
servername Optional. Specifies the server to connect to.
Default value is "localhost:3306"
username Optional. Specifies the username to log in with.
Default value is the name of the user that owns the server
password Optional. Specifies the password to log in with.
Default is ""
47
Example
• Lets store the connection in a variable ($con) for later use in the
script.
• The "die" part will be executed if the connection fails:
<?php
$con = mysqli_connect(“host",“username",“password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
mysqli_close($con);
?>
48
Example
• index.php
<fieldset>
<legend> Information </legend>
<form action="insert.php" method="post">
Name:<input type="text" name="name" required>
<p></p>
Category: <input type="text" name="category" required>
<p></p>
<input type="submit" name="submit" value="Add Student">
</form>
</fieldset>
49
Example
• dbcon.php
<?php
$host="localhost";
$username="root";
$password="";
$dbname="kana";
$con=mysqli_connect($host,$username,$password) or die
("DB Not Connected!");
mysqli_select_db($con,$dbname) or die("DB Not Selected!");
?>
50
Example
• insert.php
<?php
include('dbcon.php');
$name=$_POST['name'];
$category=$_POST['category'];
header('location:index.php');
?>
51
Example
• Write a PHP code to register new user
52
Example
• Lets add a link that displays the detail information of the users
53
Example
• Develop a simple user registration system that authenticates
users to add new users.
54
Project Title
1. Exit Exam Management System
?
END OF CHAPTER FIVE
Have a nice time