0% found this document useful (0 votes)
20 views54 pages

PHP (Hypertext Preprocessor) : 01/06/2025 1 Prepared By: Mr. Dereje A

Uploaded by

dmretab
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)
20 views54 pages

PHP (Hypertext Preprocessor) : 01/06/2025 1 Prepared By: Mr. Dereje A

Uploaded by

dmretab
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/ 54

Web Programming

CHAPTER 5

PHP (Hypertext Preprocessor)

01/06/2025 Prepared by: Mr. Dereje A. 1


Web Programming
PHP (Hypertext Preprocessor)
 PHP is an acronym for "PHP: Hypertext Preprocessor“
 PHP is a widely-used, open source scripting language
 PHP scripts are executed on the server
 PHP is free to download and use
 Implemented by Rasmus Lerdorf in 1994 using C and PERL languages.

01/06/2025 Prepared by: Mr. Dereje A. 2


Web Programming
What is a PHP File?
 PHP files can contain text, HTML, CSS, JavaScript, and PHP code
 PHP code are executed on the server, and the result is returned to the browser as plain HTML
 PHP files have extension ".php“

01/06/2025 Prepared by: Mr. Dereje A. 3


Web Programming
What Can PHP Do?
 PHP can generate dynamic page content
 PHP can create, open, read, write, delete, and close files on the server
 PHP can collect form data
 PHP can send and receive cookies
 PHP can add, delete, modify data in your database
 PHP can be used to control user-access
 PHP can encrypt data

01/06/2025 Prepared by: Mr. Dereje A. 4


Web Programming
Why PHP?
 It runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
 It is compatible with almost all servers used today (Apache, IIS, etc.)
 It supports a wide range of databases
 It is free. Download it from the official PHP resource: www.php.net
 It is easy to learn and runs efficiently on the server side
 It is very simple to learn and runs efficiently on the server side.
 It supports many databases such as MySQL, Oracle, PostgreSQL etc.
 It is perfectly suited for Web development and can be embedded directly into the HTML code.
 It can also be used to create dynamic web pages.

01/06/2025 Prepared by: Mr. Dereje A. 5


Web Programming
Features of PHP

01/06/2025 Prepared by: Mr. Dereje A. 6


Web Programming
COMMON USES OF PHP
 It is used for create dynamic website.
 To Interacting with web server (Apache etc)
 To interacting with any back-end / database server e.g. MySQL
 To implement the business logical layers (one or more)
 Access Cookies variable and set cookies
 Using php you can restrict user to access specific web page
 PHP usually used to output HTML code to the browser
 It is used for send and receive E-Mails.
 It can encrypt data.
 PHP performs system functions, i.e. from files on a system it can create, open, read, write, and
close them.
 It can handle forms, i.e. gather data from files, save data to a file.
 Many others
01/06/2025 Prepared by: Mr. Dereje A. 7
Web Programming
SOFTWARES REQUIRED FOR TO WORK WITH PHP:
1. Browser - Internet Explorer/chrome/Microsoft edge/ Mozilla/…
2. Server - Apache
3. Server side script - PHP
4. Database - MYSQL
TOOLS TO WORK WITH PHP:
5. XAMPP - Cross Apache MYSQL PHP Perl
6. WAMPP - Window Apache MYSQL PHP Perl
7. LAMPP - Linux Apache MYSQL PHP Perl
8. MAMPP - Macintosh Apache MYSQL PHP Perl

01/06/2025 Prepared by: Mr. Dereje A. 8


Web Programming
PHP Syntax:
 PHP code is start with <?php and ends with?>
 Every PHP statements end with a semicolon (;).
 PHP code save with .php extension.
 A PHP file contains HTML tags and some PHP scripting code.
 You can place PHP code any where in your document.

01/06/2025 Prepared by: Mr. Dereje A. 9


Web Programming
HOW TO WRITE PHP PROGRAM
1. Open an Editor(Visual Studio code, sublime text, notpad++, etc)
2. Create a New file
3. Save the file name with extension .php inside “htdocs” folder C:/XAMPP/htdocs(Ex: abc.php)
4. We need to write PHP code inside saved file
5. Ex: <?php -----------?>

<?php
echo" Hello World";
?>

01/06/2025 Prepared by: Mr. Dereje A. 10


Web Programming
Embedding PHP within HTML:
 PHP files are plain text files with .php extension.
 Inside a HTML file you can place php script.
 Example: first.php

01/06/2025 Prepared by: Mr. Dereje A. 11


Web Programming
PHP Execution Steps:
Step1:
 Open Mycomputer, then
 C drive, then
 Xampp, then
 Htdocs, then
 create your folder, then
 save your phptry.php file
Step2:
double click on “XAAMP CONTROL PANEL” on desktop and START “Apache”

01/06/2025 Prepared by: Mr. Dereje A. 12


Web Programming
PHP Execution Steps:
Step3:
Type localhost on your browser and press enter: It will show the following:

01/06/2025 Prepared by: Mr. Dereje A. 13


Web Programming
PHP Execution Steps:
Step4:
 Now type the following on browser: localhost/your folder name/
Step5:
Click on “phptry.php” and it will show the following:

01/06/2025 Prepared by: Mr. Dereje A. 14


Web Programming
PHP echo & Print:
echo Statement:
 echo is a statement used to display the output.
 echo can be used with or without parentheses: echo(), and echo.
 echo does not return any value.
 We can pass multiple strings separated by a comma (,) in echo.
 echo is faster than the print statement
Syntax: echo (string $arg1 [, string $... ] );
Ex:
<?php
echo “Welcome to echo statement”;
?>
01/06/2025 Prepared by: Mr. Dereje A. 15
Web Programming
PHP echo & Print:
Print Statement:
 It used as an alternative to echo to display the output.
 It can be used with or without parentheses.
 It always returns an integer value, which is 1.
 Using print, we cannot pass multiple arguments.
 print is slower than the echo statement.
 Syntax: print(string $arg)
ex:
<?php
print “Welcome to print statement”;
?>
01/06/2025 Prepared by: Mr. Dereje A. 16
Web Programming
PHP ─ VARIABLES
 The main way to store information in the middle of a PHP program is by using a
variable.
 Here are the most important things to know about variables in PHP.
 All variables in PHP are denoted with a leading dollar sign ($).
 The value of a variable is the value of its most recent assignment.
 Variables are assigned with the = operator, with the variable on the left-hand side and the expression to be
evaluated on the right.
 Variables can, but do not need, to be declared before assignment.
 Variables in PHP do not have intrinsic types - a variable does not know in advance whether it will be used
to store a number or a string of characters.
 Variables used before they are assigned have default values.
 PHP does a good job of automatically converting types from one to another when necessary.
 PHP variables are Perl-like.

01/06/2025 Prepared by: Mr. Dereje A. 17


Web Programming
PHP ─ VARIABLE Naming
 Rules for naming a variable is:
 Variable names must begin with a letter or underscore character.
 A variable name can consist of numbers, letters, underscores
 But you cannot use characters like + , - , % , ( , ) . & , etc
 There is no size limit for variables.
 Example of valid variable names:
$count;
$user_name;
$_value;
$myVariable123;

01/06/2025 Prepared by: Mr. Dereje A. 18


Web Programming
PHP ─ VARIABLE TYPES
 PHP has a total of eight data types which we use to construct our variables:
 Integers: are whole numbers, without a decimal point, like 4195.
 Doubles: are floating-point numbers, like 3.14159 or 49.1.
 Booleans: have only two possible values either true or false.
 NULL: is a special type that only has one value: NULL. E.g. $my_var = NULL;
 Strings: are sequences of characters, like “PHP supports string operations”.
 Arrays: are named and indexed collections of other values.
 Objects: are instances of programmer-defined classes, which can package up both
other kinds of values and functions that are specific to the class.
 Resources: are special variables that hold references to resources external to PHP
(such as database connections).
01/06/2025 Prepared by: Mr. Dereje A. 19
Web Programming
PHP ─ VARIABLE Scope
 Scope can be defined as the range of availability a variable has to the program in
which it is declared.
 PHP variables can be one of four scope types:
 Local variables
 Global variables
 Static variables

01/06/2025 Prepared by: Mr. Dereje A. 20


Web Programming
PHP ─ VARIABLE Scope
PHP Local Variables
 A variable declared in a function is considered local; that is, it can be referenced solely in that function.
 Any assignment outside of that function will be considered to be an entirely different variable from the
one contained in the function:
<?php
$x=4;
function assign()
{
$x=0; //local variable
print "\$x inside function is $x. "; Output: $x inside function is 0.
} $x outside of function is 4.
assignx();
print "\$x outside of function is $x. ";
?>
01/06/2025 Prepared by: Mr. Dereje A. 21
Web Programming
PHP Global Variables
 The global variables are the variables that are declared outside the function.
 These variables can be accessed anywhere in the program.
 To access the global variable within a function, use the GLOBAL keyword before the variable.
 However, these variables can be directly accessed or used outside the function without any keyword.
 Therefore there is no need to use any keyword to access a global variable outside the function.

01/06/2025 Prepared by: Mr. Dereje A. 22


Web Programming
PHP Static Variables
 When a function is completed/executed, all of its variables are deleted.
 Once it completes its execution and memory is freed.
 Sometimes we need to store a variable even after completion of function execution.
 We use the static keyword before the variable to define a variable, and this variable is called as static
variable.

01/06/2025 Prepared by: Mr. Dereje A. 23


Web Programming
PHP ─ OPERATOR TYPES
 What is Operator? Simple answer can be given using expression 4 + 5 is equal to 9.
 Here 4 and 5 are called operands and + is called operator.
 PHP language supports following type of operators.
 Arithmetic Operators
 Comparison Operators
 Logical (or Relational) Operators
 Assignment Operators
 Conditional (or ternary) Operators

01/06/2025 Prepared by: Mr. Dereje A. 24


Web Programming
PHP ─ Comments:
 PHP supports two types of comments:
 Single-line comments:
These begin with // and extend to the end of the line.
 Multi-line comments:
These start with /* and end with */.
Multi-line comments can span multiple lines and are often used for documentation.
// This is a single-line comment
/* This is a multi-line comment that spans multiple lines */

01/06/2025 Prepared by: Mr. Dereje A. 25


Web Programming
PHP- Case Sensitivity:
 The names of user-defined classes and functions, as well as built-in constructs and keywords such as
echo, while, class, etc., are case-insensitive.
 Thus, these three lines are equivalent:
echo("hello, world");
ECHO("hello, world");
EcHo("hello, world");
 Variables, on the other hand, are case-sensitive.
 That is, $name, $NAME, and $NaME are three different variables.

01/06/2025 Prepared by: Mr. Dereje A. 26


Web Programming
PHP- Semicolons:
 Statements in PHP are terminated with a semicolon ;.
 Omitting the semicolon at the end of a statement can lead to syntax errors.
$variable = 42; // Valid
echo "Hello, World"; // Valid

01/06/2025 Prepared by: Mr. Dereje A. 27


Web Programming
PHP Function Names :
 They are not case-sensitive, which means you can call them with different letter casing,
 But it's a good practice to use lowercase letters for function names.
 Function names must start with a letter or an underscore, followed by letters, numbers, and underscores.
 Example of valid function names
myFunction();
process_data();
_helperFunction();
<?php
function myFunction(lists of arguments)
{
Code to be executed
}
?>
01/06/2025 Prepared by: Mr. Dereje A. 28
Web Programming
PHP Constants:
 Constants in PHP are typically written in uppercase letters with underscores separating
words.
 By convention, they are often defined at the top of a script or in a configuration file.
 Constants are case-sensitive.
 Example of valid constants:
define('PI', 3.14159);
define('DB_HOST', 'localhost');
define('MAX_USERS', 100);
const PI=3.14;
 It's important to note that PHP is case-sensitive when it comes to variable names and class
names but not for function names.
 Therefore, you must use the correct letter casing when referencing variables and class
names.
01/06/2025 Prepared by: Mr. Dereje A. 29
Web Programming
PHP Keywords :
 Keywords in PHP are reserved words that have special meanings and purposes within the
PHP language.
 These words cannot be used as identifiers (e.g., variable names, function names, class
names)
 Here is a list of some of the most common keywords in PHP :
1. Basic Keywords:
 Echo : Used to output data to the browser.
 print : Similar to echo, used to output data.
 return: Used to return a value from a function.
 include: Used to include and evaluate a specified file.
 require: Used to include and evaluate a specified file, and it's considered fatal if the file is not found.
 include_once: Similar to include, but ensures the file is included only once.
 require_once: Similar to require, but ensures the file is included only once.
01/06/2025 Prepared by: Mr. Dereje A. 30
Web Programming
PHP Keywords :
2. Control Structures
 if: Used to conditionally execute code.
 else: Used in conjunction with if to execute code when the condition is false.
 elseif or else if: Used to add additional conditions in an if statement.
 while: Used to create a loop that executes code while a condition is true.
 for: Used to create a loop with a specific initialization, condition, and increment.
 foreach: Used to loop through arrays and objects.
 switch: Used to create a switch statement for multiple condition checks.
 case: Used within a switch statement to specify different cases.
 break: Used to exit a loop or a switch statement.
 continue: Used to skip the current iteration of a loop.
01/06/2025 Prepared by: Mr. Dereje A. 31
Web Programming
PHP Keywords :
3. Data Types:
 int, integer: Used to represent integer data types.
 float, double: Used to represent floating-point (decimal) data types.
 string: Used to represent string data types.
 bool, boolean: Used to represent Boolean data types.
 array: Used to represent arrays.
 object: Used to represent objects.
 null: Used to represent the absence of a value.

01/06/2025 Prepared by: Mr. Dereje A. 32


Web Programming
PHP Keywords :
4. Classes and Objects:
 class: Used to define a class.
 new: Used to create an instance of a class.
 this: Used to refer to the current object within a class.
 public, private, protected: Used to specify the visibility of class members (properties
and methods).
 static: Used to define static methods and properties within a class.

01/06/2025 Prepared by: Mr. Dereje A. 33


Web Programming
PHP Keywords :
6. Other Keywords:
 global: Used to access a global variable from within a function.
 const: Used to define class constants.
 define: Used to define global constants.

01/06/2025 Prepared by: Mr. Dereje A. 34


Web Programming
PHP Array Operators:

01/06/2025 Prepared by: Mr. Dereje A. 35


Web Programming
PHP Array Operators:

01/06/2025 Prepared by: Mr. Dereje A. 36


Web Programming
PHP DECISION MAKING/Control Structure:

 They have the same steps as we have been learnt in JavaScript

01/06/2025 Prepared by: Mr. Dereje A. 37


Web Programming
PHP LOOPS:
 PHP supports four different types of loops.
1. for loop
2. while loop Has the same structure as in JavaScript
3. do-while loop
4. foreach loop

01/06/2025 Prepared by: Mr. Dereje A. 38


Web Programming
PHP LOOPS:
foreach loop:
 The foreach statement is used to loop through arrays.
 For each pass the value of the current array element is assigned to $value and the array pointer is moved
by one and in the next pass next element will be processed.
Syntax:
foreach (array_element as value)
{ //code to be executed
}

01/06/2025 Prepared by: Mr. Dereje A. 39


Web Programming
GET vs. POST
 Both GET and POST create an array (e.g. array( key1 => value1, key2 => value2, key3 => value3, ...)).
 This array holds key/value pairs, where keys are the names of the form controls and values are the input
data from the user.
 Both GET and POST are treated as $_GET and $_POST.
 These are superglobals, which means that they are always accessible, regardless of scope.
 You can access them from any function, class or file without having to do anything special.
 $_GET is an array of variables passed to the current script via the URL parameters.
 $_POST is an array of variables passed to the current script via the HTTP POST method.

01/06/2025 Prepared by: Mr. Dereje A. 40


Web Programming
When to use GET?
 Information sent from a form with the GET method is visible to everyone (all variable names and
values are displayed in the URL).
 GET also has limits on the amount of information to send.
 The limitation is about 2000 characters.
 However, because the variables are displayed in the URL, it is possible to bookmark the page.
 This can be useful in some cases.
 GET may be used for sending non-sensitive data.
 Note: GET should NEVER be used for sending passwords or other sensitive information!

01/06/2025 Prepared by: Mr. Dereje A. 41


Web Programming
When to use POST?
 Information sent from a form with the POST method is invisible to others
 All names/values are embedded within the body of the HTTP request) and has no limits on the amount
of information to send.
 Moreover POST supports advanced functionality such as support for multi-part binary input while
uploading files to server.
 However, because the variables are not displayed in the URL, it is not possible to bookmark the page.
 Developers prefer POST for sending form data.

01/06/2025 Prepared by: Mr. Dereje A. 42


Web Programming
PHP Form Handling
 The PHP superglobals $_GET and $_POST are used to collect form-data.
 The example below displays a simple HTML form with two input fields and a submit
button:

01/06/2025 Prepared by: Mr. Dereje A. 43


Web Programming
PHP File Uploading
 Uploading files to a server using PHP is a common task that can be accomplished with a few steps.
 However, it's important to handle file uploads securely to prevent potential security risks.

01/06/2025 Prepared by: Mr. Dereje A. 44


Web Programming
Create Cookies With PHP
 A cookie is created with the setcookie() function.

 The following example creates a cookie named "user" with the value “ John Doe".
 The cookie will expire after 30 days (86400 * 30).
 The "/" means that the cookie is available in entire website (otherwise, select the directory you prefer).
 We then retrieve the value of the cookie "user" (using the global variable $_COOKIE).
 We also use the isset() function to find out if the cookie is set:

01/06/2025 Prepared by: Mr. Dereje A. 45


Web Programming
Create Cookies With PHP

01/06/2025 Prepared by: Mr. Dereje A. 46


Web Programming
Connect PHP to MySQL
What is MySQL?
 MySQL is an open-source relational database management system (RDBMS).
 It is the most popular database system used with PHP.
 Structured Query Language (SQL).
 The data in a MySQL database are stored in tables that consist of columns and rows.
 MySQL is a database system that runs on a server.
 MySQL is ideal for both small and large applications.
 MySQL is a very fast, reliable, and easy-to-use database system.
 It uses standard SQL.
 MySQL compiles on a number of platforms.

01/06/2025 Prepared by: Mr. Dereje A. 47


Web Programming
Connect PHP to MySQL

01/06/2025 Prepared by: Mr. Dereje A. 48


Web Programming
MySQL Query in PHP
Creating Database

01/06/2025 Prepared by: Mr. Dereje A. 49


Web Programming
MySQL Query in PHP
Creating Table

01/06/2025 Prepared by: Mr. Dereje A. 50


Web Programming
MySQL Query in PHP
Data Inserting

01/06/2025 Prepared by: Mr. Dereje A. 51


Web Programming
MySQL Query in PHP
Fetching Data

01/06/2025 Prepared by: Mr. Dereje A. 52


Web Programming

01/06/2025 Prepared by: Mr. Dereje A. 53


END OF
CHAPTER
FIVE
01/06/2025 Prepared by Mr. Dereje A. 54

You might also like