SlideShare a Scribd company logo
1 of 47
Download to read offline
IS333 Web based information systems
By. Shaimaa Mohamed Galal
PHP : Hypertext Preprocessor
Outline
What is PHP?
What can PHP do?
Why PHP?
Setup your PHP on your own PC.
PHP Basic syntax.
PHP Variables scope.
PHP Arrays.
PHP Forms.
PHP Functions.
What is a PHP?
⦁ PHP stands for PHP: Hypertext Preprocessor
⦁ PHP files can contain text, HTML, JavaScript code,
and PHP code
⦁ PHP code are executed on the server, and the result
is returned to the browser as plain HTML
⦁ PHP files have a default file extension of ".php"
What Can PHP Do?
⦁ PHP can generate dynamic page content
⦁ PHP can create, open, read, write, 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 restrict users to access some pages on
your website
⦁ PHP can encrypt data
Why PHP?
⦁ PHP runs on different platforms (Windows, Linux,
Unix, Mac OS X, etc.)
⦁ PHP is compatible with almost all servers used today
(Apache, IIS, etc.)
⦁ PHP has support for a wide range of databases
⦁ PHP is free. Download it from the official PHP
resource: www.php.net
⦁ PHP is easy to learn and runs efficiently on the
server side
Set Up PHP on Your Own PC
⦁ We use local computer and PHP is a server sider
language:
⦁ You need to install a web server (Localhost)
⦁ Install XAMPP (https://www.apachefriends.org/)
⦁ XAMPP provide MySQL DB engine
⦁ YouTube video of how
to install XAMPP
⦁ https://www.youtube.co
m/watch?v=mXdpCRg
R-xE
XAMPP control panel and configuration
XAMPP control panel and configuration
PHPMyAdmin (localhost/phpmyadmin/)
PHP Editors
⦁ A lot of PHP are available online.
⦁ RapidPHP: is a lightweight PHP editor and IDE
30 Days
Trial
PHP Editors
⦁ Atom: is a lightweight PHP editor
Free
Basic PHP Syntax
A PHP script can be placed anywhere in the
document. A PHP script starts with <?php and ends
with ?>:
<?php
// PHP code goes here
?>
The default file extension for PHP files is ".php".
Comments in PHP
⦁ Example
<html>
<body>
<?php
//This is a PHP comment line
/*
This is a PHP comment
block
*/
?>
</body>
</html>
PHP Variables
⦁ Variable can have short names (like x and y) or more
descriptive names (age, carname, totalvolume).
⦁ Rules for PHP variables:
▪ A variable starts with the $ sign, followed by the name of
the variable
▪ A 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 spaces
▪ Variable names are case sensitive ($y and $Y are two
different variables)
Creating (Declaring) PHP Variables
⦁ PHP has no command for declaring a variable.
⦁ A variable is created the moment you first assign a
value to it:
⦁ $txt="Hello world!";
$x=5;
⦁ After the execution of the statements above, the
variable txt will hold the value Hello world!, and the
variable xwill hold the value 5.
⦁ Note: When you assign a text value to a variable,
put quotes around the value.
PHP is a Loosely Typed Language
⦁ In the example above, notice that we did not have to
tell PHP which data type the variable is.
⦁ PHP automatically converts the variable to the
correct data type, depending on its value.
⦁ In a strongly typed programming language, we will
have to declare (define) the type and name of the
variable before using it.
PHP Variable Scopes
⦁ The scope of a variable is the part of the script
where the variable can be referenced/used.
⦁ PHP has four different variable scopes:
⦁ local
⦁ global
⦁ static
⦁ parameter
Local Scope
⦁ A variable declared within a PHP function is local
and can only be accessed within that function:
⦁ Example
<?php
$x=5; // global scope
function myTest()
{
echo $x; // local scope
}
myTest();
?>
Global Scope
⦁ A variable that is defined outside of any function,
has a global scope.
⦁ Global variables can be accessed from any part of
the script, EXCEPT from within a function.
⦁ To access a global variable from within a function,
use the global keyword:
Global Scope
⦁ Example
⦁ <?php
$x=5; // global scope
$y=10; // global scope
function myTest()
{
global $x,$y;
$y=$x+$y;
}
myTest();
echo $y; // outputs 15
?>
PHP Superglobals (Global Variables)
• Predefined variables in PHP are "superglobals“.
• They are always accessible, regardless of
scope - and you can access them from any
function, class or file without having to do
anything special.
• The PHP superglobal variables are:
•$GLOBALS
•$_SERVER
•$_REQUEST
•$_POST
•$_GET
•$_FILES
•$_ENV
•$_COOKIE
•$_SESSION
Static Scope
⦁ When a function is completed, all of its variables are
normally deleted. However, sometimes you want a
local variable to not be deleted.
⦁ To do this, use the static keyword when you first
declare the variable:
Static Scope
⦁ Example
<?php
function myTest()
{
static $x=0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>
🞂 OUTPUT ?
Parameter Scope
⦁ A parameter is a local variable whose value is
passed to the function by the calling code.
⦁ Parameters are declared in a parameter list as part
of the function declaration:
⦁ Example : <?php
function myTest($x)
{
echo $x;
}
myTest(5);
?>
PHP echo and print Statements
⦁ In PHP there is two basic ways to get output:
echo and print.
⦁ There are some differences between echo and
print:
⦁ echo - can output one or more strings
⦁ print - can only output one string, and returns always 1
⦁ Tip: echo is marginally faster compared to print
as echo does not return any value.
<?php
echo "<h2>PHP is fun!</h2>";
echo "Hello world!<br>";
echo "This", " string", " was", " made", " with multiple
parameters.";
?>
PHP Arrays
⦁ An array stores multiple values in one single
variable:
Example
<?php
$cars = array("Volvo","BMW","Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " .
$cars[2] . ".";
?>
Create an Array in PHP
• In PHP, the array() function is used to create an
array:
• Example :
$cars = array("Volvo","BMW","Toyota");
• In PHP, there are three types of arrays:
▪ Indexed arrays - Arrays with numeric index
▪ Associative arrays - Arrays with named keys
▪ Multidimensional arrays - Arrays containing one or more
arrays
PHP Indexed Arrays
⦁ There are two ways to create indexed arrays:
⦁ The index can be assigned automatically (index
always starts at 0):
⦁ $cars=array("Volvo","BMW","Toyota");
OR
⦁ The index can be assigned manually:
$cars[0]="Volvo";
$cars[1]="BMW";
$cars[2]="Toyota";
PHP Associative Arrays
⦁ Associative arrays are arrays that use named keys
that you assign to them.
⦁ There are two ways to create an associative array:
⦁ $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43
");
OR
⦁ $age['Peter']="35";
$age['Ben']="37";
$age['Joe']="43";
PHP Associative Arrays
⦁ Example
<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
// Output : Peter is 35 years old.
PHP Multidimensional Arrays
➢ An array can also contain another array as a value,
which in turn can hold other arrays as well. In such a
way we can create two- or three-dimensional arrays:
Example
<?php
// A two-dimensional array:
$cars = array
(
array("Volvo",100,96),
array("BMW",60,59),
array("Toyota",110,100)
);
?>
The foreach Loop
• The foreach loop works only on arrays, and is used
to loop through each key/value pair in an array.
foreach ($array as $value)
{
code to be executed;
}
For every loop iteration, the value of the
current array element is assigned to
$value (and the array pointer is moved by
one) - so on the next loop iteration, you'll
be looking at the next array value.
The foreach Loop
Example:
<?php
$x=array("one","two","three");
foreach ($x as $value)
{
echo $value . "<br>";
}
?>
The foreach Loop
In PHP, we have the following loop types:
• while - loops through a block of code as long as
the specified condition is true
• do...while - loops through a block of code once,
and then repeats the loop as long as the specified
condition is true
• for - loops through a block of code a specified
number of times
• foreach - loops through a block of code for each
element in an array
PHP Form Handling
<p>Guessing game...</p>
<form>
<p><label for="guess">Input
Guess</label>
<input type="text" name="guess"/></p>
<input type="submit"/>
</form>
form1.php
$_GET and $_POST
• PHP loads the values for the URL parameters into
an array called $_GET and the POST parameters
into an array called $_POST
• There is another array called $_REQUEST which
merges GET and POST data
<p>Guessing game...</p>
<form>
<p><label for="guess">Input
Guess</label>
<input type="text" name="guess"></p>
<input type="submit"/>
</form>
<?php
print_r($_GET);
?>
form2.php
<p>Guessing game...</p>
<form method="post">
<p><label for="guess">Input
Guess</label>
<input type="text" name="guess"
size="40"/></p>
<input type="submit"/>
</form>
<pre>
$_POST:
<?php
print_r($_POST);
?>
$_GET:
<?php
print_r($_GET);
?>
form3.php
PHP $_GET Variable
• The predefined $_GET variable is used to collect
values in a form with method="get"
• Information sent from a form with the GET method is
visible to everyone (it will be displayed in the
browser's address bar) and has limits on the amount
of information to send.
• Example
<form action="welcome.php" method="get">
Name: <input type="text" name="fname">
Age: <input type="text" name="age">
<input type="submit">
</form>
• When the user clicks the "Submit" button, the URL sent
to the server could look something like this:
http://www.w3schools.com/welcome.php?fname=Peter&age=37
• The "welcome.php" file can now use the $_GET variable
to collect form data as follow:
PHP $_GET Variable
Welcome <?php echo $_GET["fname"]; ?>.<br>
You are <?php echo $_GET["age"]; ?> years old!
The $_POST Variable
• The predefined $_POST variable is used to collect
values from a form sent with method="post".
• Information sent from a form with the POST method
is invisible to others and has no limits on the amount
of information to send.
• Note: However, there is an 8 MB max size for the
POST method, by default (can be changed by
setting the post_max_size in the php.ini file).
GET Vs POST
PHP Functions
⦁ PHP Defined Functions
⦁ Built-in PHP functions,
⦁ A function will be executed by a call to the function.
PHP Functions – Built in Functions
PHP Functions
⦁ PHP User Defined Functions
⦁ Besides the built-in PHP functions, we can create our own
functions.
⦁ A function is a block of statements that can be used repeatedly in a
program.
⦁ A function will not execute immediately when a page loads.
⦁ A function will be executed by a call to the function.
⦁ Create a User Defined Function in PHP
⦁ A user defined function declaration starts with the word "function":
Syntax
function functionName(arg1, arg2…)
{
code to be executed;
}
⦁ Note: A function name can start with a letter or underscore (not a
number).
PHP Functions – User Defined Functions
PHP tutorials
W3schools
https://www.w3schools.com/php/
YouTube Channel (Dani Krossings) Procedural PHP
https://www.youtube.com/watch?v=qVU3V0A05k8&list
=PL0eyrZgxdwhwBToawjm9faF1ixePexft-

More Related Content

Similar to Lecture2_IntroductionToPHP_Spring2023.pdf

Similar to Lecture2_IntroductionToPHP_Spring2023.pdf (20)

php 1
php 1php 1
php 1
 
Basic php 5
Basic php 5Basic php 5
Basic php 5
 
WT_PHP_PART1.pdf
WT_PHP_PART1.pdfWT_PHP_PART1.pdf
WT_PHP_PART1.pdf
 
PHP and MySQL.ppt
PHP and MySQL.pptPHP and MySQL.ppt
PHP and MySQL.ppt
 
Introduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPIntroduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHP
 
Wt unit 4 server side technology-2
Wt unit 4 server side technology-2Wt unit 4 server side technology-2
Wt unit 4 server side technology-2
 
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
Php mysql classes in navi-mumbai,php-mysql course provider-in-navi-mumbai,bes...
 
PHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERSPHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERS
 
Php
PhpPhp
Php
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
 
Php a dynamic web scripting language
Php   a dynamic web scripting languagePhp   a dynamic web scripting language
Php a dynamic web scripting language
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 
Php
PhpPhp
Php
 
Php
PhpPhp
Php
 
Php
PhpPhp
Php
 
php Chapter 1.pptx
php Chapter 1.pptxphp Chapter 1.pptx
php Chapter 1.pptx
 
Materi Dasar PHP
Materi Dasar PHPMateri Dasar PHP
Materi Dasar PHP
 
FYBSC IT Web Programming Unit IV PHP and MySQL
FYBSC IT Web Programming Unit IV  PHP and MySQLFYBSC IT Web Programming Unit IV  PHP and MySQL
FYBSC IT Web Programming Unit IV PHP and MySQL
 
1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master
 

More from ShaimaaMohamedGalal

Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....ShaimaaMohamedGalal
 
Data mining ..... Association rule mining
Data mining ..... Association rule miningData mining ..... Association rule mining
Data mining ..... Association rule miningShaimaaMohamedGalal
 
Lecture8_AdvancedPHP(Continue)-APICalls_SPring2023.pdf
Lecture8_AdvancedPHP(Continue)-APICalls_SPring2023.pdfLecture8_AdvancedPHP(Continue)-APICalls_SPring2023.pdf
Lecture8_AdvancedPHP(Continue)-APICalls_SPring2023.pdfShaimaaMohamedGalal
 
Lecture15_LaravelGetStarted_SPring2023.pdf
Lecture15_LaravelGetStarted_SPring2023.pdfLecture15_LaravelGetStarted_SPring2023.pdf
Lecture15_LaravelGetStarted_SPring2023.pdfShaimaaMohamedGalal
 
Lecture11_LaravelGetStarted_SPring2023.pdf
Lecture11_LaravelGetStarted_SPring2023.pdfLecture11_LaravelGetStarted_SPring2023.pdf
Lecture11_LaravelGetStarted_SPring2023.pdfShaimaaMohamedGalal
 
1. Lecture1_NOSQL_Introduction.pdf
1. Lecture1_NOSQL_Introduction.pdf1. Lecture1_NOSQL_Introduction.pdf
1. Lecture1_NOSQL_Introduction.pdfShaimaaMohamedGalal
 

More from ShaimaaMohamedGalal (10)

Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....
 
Data mining ..... Association rule mining
Data mining ..... Association rule miningData mining ..... Association rule mining
Data mining ..... Association rule mining
 
Lecture 0 - Advanced DB.pdf
Lecture 0 - Advanced DB.pdfLecture 0 - Advanced DB.pdf
Lecture 0 - Advanced DB.pdf
 
Lecture8_AdvancedPHP(Continue)-APICalls_SPring2023.pdf
Lecture8_AdvancedPHP(Continue)-APICalls_SPring2023.pdfLecture8_AdvancedPHP(Continue)-APICalls_SPring2023.pdf
Lecture8_AdvancedPHP(Continue)-APICalls_SPring2023.pdf
 
Lecture15_LaravelGetStarted_SPring2023.pdf
Lecture15_LaravelGetStarted_SPring2023.pdfLecture15_LaravelGetStarted_SPring2023.pdf
Lecture15_LaravelGetStarted_SPring2023.pdf
 
Lecture11_LaravelGetStarted_SPring2023.pdf
Lecture11_LaravelGetStarted_SPring2023.pdfLecture11_LaravelGetStarted_SPring2023.pdf
Lecture11_LaravelGetStarted_SPring2023.pdf
 
Lecture9_OOPHP_SPring2023.pptx
Lecture9_OOPHP_SPring2023.pptxLecture9_OOPHP_SPring2023.pptx
Lecture9_OOPHP_SPring2023.pptx
 
2. Lecture2_NOSQL_KeyValue.ppt
2. Lecture2_NOSQL_KeyValue.ppt2. Lecture2_NOSQL_KeyValue.ppt
2. Lecture2_NOSQL_KeyValue.ppt
 
1. Lecture1_NOSQL_Introduction.pdf
1. Lecture1_NOSQL_Introduction.pdf1. Lecture1_NOSQL_Introduction.pdf
1. Lecture1_NOSQL_Introduction.pdf
 
Lecture3.ppt
Lecture3.pptLecture3.ppt
Lecture3.ppt
 

Recently uploaded

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 

Recently uploaded (20)

#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 

Lecture2_IntroductionToPHP_Spring2023.pdf

  • 1. IS333 Web based information systems By. Shaimaa Mohamed Galal PHP : Hypertext Preprocessor
  • 2. Outline What is PHP? What can PHP do? Why PHP? Setup your PHP on your own PC. PHP Basic syntax. PHP Variables scope. PHP Arrays. PHP Forms. PHP Functions.
  • 3. What is a PHP? ⦁ PHP stands for PHP: Hypertext Preprocessor ⦁ PHP files can contain text, HTML, JavaScript code, and PHP code ⦁ PHP code are executed on the server, and the result is returned to the browser as plain HTML ⦁ PHP files have a default file extension of ".php"
  • 4. What Can PHP Do? ⦁ PHP can generate dynamic page content ⦁ PHP can create, open, read, write, 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 restrict users to access some pages on your website ⦁ PHP can encrypt data
  • 5. Why PHP? ⦁ PHP runs on different platforms (Windows, Linux, Unix, Mac OS X, etc.) ⦁ PHP is compatible with almost all servers used today (Apache, IIS, etc.) ⦁ PHP has support for a wide range of databases ⦁ PHP is free. Download it from the official PHP resource: www.php.net ⦁ PHP is easy to learn and runs efficiently on the server side
  • 6. Set Up PHP on Your Own PC ⦁ We use local computer and PHP is a server sider language: ⦁ You need to install a web server (Localhost) ⦁ Install XAMPP (https://www.apachefriends.org/) ⦁ XAMPP provide MySQL DB engine ⦁ YouTube video of how to install XAMPP ⦁ https://www.youtube.co m/watch?v=mXdpCRg R-xE
  • 7. XAMPP control panel and configuration
  • 8. XAMPP control panel and configuration
  • 10. PHP Editors ⦁ A lot of PHP are available online. ⦁ RapidPHP: is a lightweight PHP editor and IDE 30 Days Trial
  • 11. PHP Editors ⦁ Atom: is a lightweight PHP editor Free
  • 12. Basic PHP Syntax A PHP script can be placed anywhere in the document. A PHP script starts with <?php and ends with ?>: <?php // PHP code goes here ?> The default file extension for PHP files is ".php".
  • 13. Comments in PHP ⦁ Example <html> <body> <?php //This is a PHP comment line /* This is a PHP comment block */ ?> </body> </html>
  • 14. PHP Variables ⦁ Variable can have short names (like x and y) or more descriptive names (age, carname, totalvolume). ⦁ Rules for PHP variables: ▪ A variable starts with the $ sign, followed by the name of the variable ▪ A 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 spaces ▪ Variable names are case sensitive ($y and $Y are two different variables)
  • 15. Creating (Declaring) PHP Variables ⦁ PHP has no command for declaring a variable. ⦁ A variable is created the moment you first assign a value to it: ⦁ $txt="Hello world!"; $x=5; ⦁ After the execution of the statements above, the variable txt will hold the value Hello world!, and the variable xwill hold the value 5. ⦁ Note: When you assign a text value to a variable, put quotes around the value.
  • 16. PHP is a Loosely Typed Language ⦁ In the example above, notice that we did not have to tell PHP which data type the variable is. ⦁ PHP automatically converts the variable to the correct data type, depending on its value. ⦁ In a strongly typed programming language, we will have to declare (define) the type and name of the variable before using it.
  • 17. PHP Variable Scopes ⦁ The scope of a variable is the part of the script where the variable can be referenced/used. ⦁ PHP has four different variable scopes: ⦁ local ⦁ global ⦁ static ⦁ parameter
  • 18. Local Scope ⦁ A variable declared within a PHP function is local and can only be accessed within that function: ⦁ Example <?php $x=5; // global scope function myTest() { echo $x; // local scope } myTest(); ?>
  • 19. Global Scope ⦁ A variable that is defined outside of any function, has a global scope. ⦁ Global variables can be accessed from any part of the script, EXCEPT from within a function. ⦁ To access a global variable from within a function, use the global keyword:
  • 20. Global Scope ⦁ Example ⦁ <?php $x=5; // global scope $y=10; // global scope function myTest() { global $x,$y; $y=$x+$y; } myTest(); echo $y; // outputs 15 ?>
  • 21. PHP Superglobals (Global Variables) • Predefined variables in PHP are "superglobals“. • They are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special. • The PHP superglobal variables are: •$GLOBALS •$_SERVER •$_REQUEST •$_POST •$_GET •$_FILES •$_ENV •$_COOKIE •$_SESSION
  • 22. Static Scope ⦁ When a function is completed, all of its variables are normally deleted. However, sometimes you want a local variable to not be deleted. ⦁ To do this, use the static keyword when you first declare the variable:
  • 23. Static Scope ⦁ Example <?php function myTest() { static $x=0; echo $x; $x++; } myTest(); myTest(); myTest(); ?> 🞂 OUTPUT ?
  • 24. Parameter Scope ⦁ A parameter is a local variable whose value is passed to the function by the calling code. ⦁ Parameters are declared in a parameter list as part of the function declaration: ⦁ Example : <?php function myTest($x) { echo $x; } myTest(5); ?>
  • 25. PHP echo and print Statements ⦁ In PHP there is two basic ways to get output: echo and print. ⦁ There are some differences between echo and print: ⦁ echo - can output one or more strings ⦁ print - can only output one string, and returns always 1 ⦁ Tip: echo is marginally faster compared to print as echo does not return any value. <?php echo "<h2>PHP is fun!</h2>"; echo "Hello world!<br>"; echo "This", " string", " was", " made", " with multiple parameters."; ?>
  • 26. PHP Arrays ⦁ An array stores multiple values in one single variable: Example <?php $cars = array("Volvo","BMW","Toyota"); echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; ?>
  • 27. Create an Array in PHP • In PHP, the array() function is used to create an array: • Example : $cars = array("Volvo","BMW","Toyota"); • In PHP, there are three types of arrays: ▪ Indexed arrays - Arrays with numeric index ▪ Associative arrays - Arrays with named keys ▪ Multidimensional arrays - Arrays containing one or more arrays
  • 28. PHP Indexed Arrays ⦁ There are two ways to create indexed arrays: ⦁ The index can be assigned automatically (index always starts at 0): ⦁ $cars=array("Volvo","BMW","Toyota"); OR ⦁ The index can be assigned manually: $cars[0]="Volvo"; $cars[1]="BMW"; $cars[2]="Toyota";
  • 29. PHP Associative Arrays ⦁ Associative arrays are arrays that use named keys that you assign to them. ⦁ There are two ways to create an associative array: ⦁ $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43 "); OR ⦁ $age['Peter']="35"; $age['Ben']="37"; $age['Joe']="43";
  • 30. PHP Associative Arrays ⦁ Example <?php $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); echo "Peter is " . $age['Peter'] . " years old."; ?> // Output : Peter is 35 years old.
  • 31. PHP Multidimensional Arrays ➢ An array can also contain another array as a value, which in turn can hold other arrays as well. In such a way we can create two- or three-dimensional arrays: Example <?php // A two-dimensional array: $cars = array ( array("Volvo",100,96), array("BMW",60,59), array("Toyota",110,100) ); ?>
  • 32. The foreach Loop • The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. foreach ($array as $value) { code to be executed; } For every loop iteration, the value of the current array element is assigned to $value (and the array pointer is moved by one) - so on the next loop iteration, you'll be looking at the next array value.
  • 33. The foreach Loop Example: <?php $x=array("one","two","three"); foreach ($x as $value) { echo $value . "<br>"; } ?>
  • 34. The foreach Loop In PHP, we have the following loop types: • while - loops through a block of code as long as the specified condition is true • do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true • for - loops through a block of code a specified number of times • foreach - loops through a block of code for each element in an array
  • 35. PHP Form Handling <p>Guessing game...</p> <form> <p><label for="guess">Input Guess</label> <input type="text" name="guess"/></p> <input type="submit"/> </form> form1.php
  • 36. $_GET and $_POST • PHP loads the values for the URL parameters into an array called $_GET and the POST parameters into an array called $_POST • There is another array called $_REQUEST which merges GET and POST data
  • 37. <p>Guessing game...</p> <form> <p><label for="guess">Input Guess</label> <input type="text" name="guess"></p> <input type="submit"/> </form> <?php print_r($_GET); ?> form2.php
  • 38. <p>Guessing game...</p> <form method="post"> <p><label for="guess">Input Guess</label> <input type="text" name="guess" size="40"/></p> <input type="submit"/> </form> <pre> $_POST: <?php print_r($_POST); ?> $_GET: <?php print_r($_GET); ?> form3.php
  • 39. PHP $_GET Variable • The predefined $_GET variable is used to collect values in a form with method="get" • Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar) and has limits on the amount of information to send. • Example <form action="welcome.php" method="get"> Name: <input type="text" name="fname"> Age: <input type="text" name="age"> <input type="submit"> </form>
  • 40. • When the user clicks the "Submit" button, the URL sent to the server could look something like this: http://www.w3schools.com/welcome.php?fname=Peter&age=37 • The "welcome.php" file can now use the $_GET variable to collect form data as follow: PHP $_GET Variable Welcome <?php echo $_GET["fname"]; ?>.<br> You are <?php echo $_GET["age"]; ?> years old!
  • 41. The $_POST Variable • The predefined $_POST variable is used to collect values from a form sent with method="post". • Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send. • Note: However, there is an 8 MB max size for the POST method, by default (can be changed by setting the post_max_size in the php.ini file).
  • 43. PHP Functions ⦁ PHP Defined Functions ⦁ Built-in PHP functions, ⦁ A function will be executed by a call to the function.
  • 44. PHP Functions – Built in Functions
  • 45. PHP Functions ⦁ PHP User Defined Functions ⦁ Besides the built-in PHP functions, we can create our own functions. ⦁ A function is a block of statements that can be used repeatedly in a program. ⦁ A function will not execute immediately when a page loads. ⦁ A function will be executed by a call to the function. ⦁ Create a User Defined Function in PHP ⦁ A user defined function declaration starts with the word "function": Syntax function functionName(arg1, arg2…) { code to be executed; } ⦁ Note: A function name can start with a letter or underscore (not a number).
  • 46. PHP Functions – User Defined Functions
  • 47. PHP tutorials W3schools https://www.w3schools.com/php/ YouTube Channel (Dani Krossings) Procedural PHP https://www.youtube.com/watch?v=qVU3V0A05k8&list =PL0eyrZgxdwhwBToawjm9faF1ixePexft-