0% found this document useful (0 votes)
6 views48 pages

Intro To PHP

This document provides an introduction to PHP, detailing its history, core features, and installation process. It covers PHP's syntax, variable types, scope, constants, control statements, and superglobals like $_SERVER and $_POST. Additionally, it includes practical activities for applying PHP concepts, such as creating a robot fuel checker and a mystery locker security system.

Uploaded by

Marcus Bautista
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)
6 views48 pages

Intro To PHP

This document provides an introduction to PHP, detailing its history, core features, and installation process. It covers PHP's syntax, variable types, scope, constants, control statements, and superglobals like $_SERVER and $_POST. Additionally, it includes practical activities for applying PHP concepts, such as creating a robot fuel checker and a mystery locker security system.

Uploaded by

Marcus Bautista
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/ 48

INTRO TO PHP

Hypertext Preprocessor
Brief History
PHP is a general-purpose scripting language that has become
one of the most widely used programming languages on the
web. Despite new languages and technologies emerging in
recent years, PHP remains essential for web development
due to its flexibility, ease of use, and open-source nature.
Brief History
PHP was created in 1994 by Rasmus Lerdorf written
in C. Initially,
Lerdorf developed PHP to track visits to his onlin
e résumé
, referring to it as "Personal Home Page Tools" (hence
the name PHP). It wasn’t long before PHP expanded
beyond Lerdorf’s original use case, evolving into a
more robust scripting language for developing
dynamic web pages.

In 1995, Lerdorf released PHP/FI (Personal Home Page /


Forms Interpreter), which included support for form
handling and databases.
Core Features of PHP
• Server-side Scripting - PHP executes on the
server, meaning the processing takes place on the
web server, not in the browser. This allows for
dynamic web pages and forms with personalized
content.

• Embedded in HTML: PHP can be easily embedded


within HTML, making it a perfect language for web
developers who want to add server-side
functionality without having to switch to another
language
Core Features of PHP
• Database Integration: PHP can interact
seamlessly with databases, making it ideal for
dynamic websites that require the storage and
retrieval of data, such as user accounts, product
listings, and blogs.

• Cross-Platform Compatibility: PHP runs on all


major operating systems, including Windows, Linux,
and macOS, and works with most web servers like
Apache, IIS, and Nginx.
How PHP works?
PHP is a server-side scripting language that creates
dynamic content for websites.
How it differs from other
languages?
PHP Installation
There are many precompiled bundles available both in
open-source as well as proprietary distributions.
XAMPP, from Apache Friends (
https://www.apachefriends.org/) is one of the most
popular PHP enabled web server packages. XAMPP is
an easy to install Apache distribution that
contains Apache, MariaDB, PHP and Perl. The
letter X in the acronym indicates that it is a cross-
platform software, available for use on Windows, Linux
and OS X. Note that XAMPP includes MariaDB, which is
a fork of MySQL, with no difference in its functionality.
PHP Installation
Make sure to install in drive C:\ Once finished, uncheck this:
(do not modify the location)
PHP Installation
1. Go to the folder location c:\xampp
2. Run xampp-control.exe by double-clicking this exe file

3. Or simply search Xampp Control Panel. Start Apache and


MySQL
PHP Installation
Inside xampp folder, find htdocs. If you visit localhost in your
browser, everything you see is what’s inside htdocs. So you can
delete all the files inside htdocs as we don’t need this. Pin this
to Quick Access.
Creating your Directory
Inside htdocs, you may now create your parent directory. Open
this folder in your IDE (visual studio code) and create your first
php file:
Installing PHP to VS Code
If you run to this error in VS Code:

Go to File > Preferences > Setting > Search for Extensions


> php
Installing PHP to VS Code
Click Edit in settings.json, we need to link our php
executable file. Once done, save and close the file.
Test Run:
Inside index.php, create a basic html document structure, then
inside body:

Access your website and refresh the page.


PHP Syntax
Basic Syntax
Canonical PHP tags are commonly used to include PHP code
within an HTML file. These tags start with '<?php' and end with
'?>'.

Escaping from HTML


The PHP parser ignores
everything outside of a pair
of opening and closing tags.
Thus, a PHP file can have
mixed content. This allows
PHP to be embedded in
HTML documents −
Variable
Variables
• Variables in PHP are used to store data that can be
accessed and modified across the program.
• One of PHP's unique features is that it is a loosely
typed language, which means you are not required
to declare the data type of a variable when you
create it. PHP defines the variable's type based on
the value assigned to it.
Variables
To declare a variable in PHP, just assign a value by
typing the $ symbol followed by the variable name.
PHP variables are case-sensitive and should begin with
a letter or an underscore, followed by any number of
letters, numbers or underscores.
Variable Rules
• A variable must start with a $ symbol, then its name.
• The variable name must start with a letter or underscore (_).
• A variable name cannot start with a number.
• The variable name can contain letters, digits or underscores.
• PHP is case sensitive, so $Name and $name are distinct
variables.
Variable Types

Get the Type


To get the data type of a variable, use
the var_dump() function.
Variables Scope
Scope can be defined as the range of availability a variable has to
the program in which it is declared.

• Local Variables
• Global Variables
• Static Variables
• Function Parameters
Local Variable
• 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. You cannot access them outside of the function.
Why Use Local Variable?

• Security: Local variables protect data within


functions.
• Memory Management: After use, local variables are
deleted to save memory.
• Avoid conflict: Local variables with the same name
in different functions do not interact with each other.
Global Variable
In PHP, any variable that can be accessed from anywhere in a PHP
script is called as a global variable. If the variable is declared
outside all the functions or classes in the script, it becomes a
global variable.

While global variables can be accessed directly outside a function,


they aren't automatically available inside a function.
• Accessible globally, but internal operations need particular
handling
• Use the $GLOBALS array or the global keyword to access
functions.
Global Variable
In the script below, $name is
global for the function sayhello().

However, the variable is not


accessible inside the function.
Hence, you will get an error
message "Undefined variable
$name".
Global Variable

To get access within a function, you


need to use the "global" keyword
before the variable.

If a function accesses a global


variable and modifies it, the modified
value is available everywhere after
the function call is completed.
The $GLOBALS Array

PHP maintains an associative array named $GLOBALS that holds


all the variables and their values declared in a global scope. The
$GLOBALS array also stores many predefined variables called as
superglobals, along with the user defined global variables.

Any of the global variables can also be accessed inside any


function with the help of a regular syntax of accessing an arrow
element. For example, the value of the global variable $name is
given by $GLOBALS["name"].
The $GLOBALS Array

In the following example, two


global variable $x and $y are
accessed inside the addition()
function.

It will produce:
The $GLOBALS Array
You can also add any local variable into the global scope by
adding it in the $GLOBALS array. Let us add $z in the global
scope.
Constants
A constant in PHP is a name or an identifier for a simple value.
A constant value cannot change during the execution of the
PHP script.
Defining a Constants
We can use the define() function to create a constant in PHP
programming language.
Rules for Defining a Constants
Here are the rules listed below for defining a constant
Type Casting
The term "Type Casting" refers to conversion of one type of
data to another. Since PHP is a weakly typed language, the
parser coerces certain data types into others while performing
certain operations.
To convert an expression of one type to another, you
need to put the data type of the latter in parenthesis
before the expression.
Control Statements
Decision Making Structure
Using endif in PHP
PHP code is usually
intermixed with HTML script.
We can insert HTML code in
the if part as well as the else
part in PHP code. PHP offers
an alternative syntax for if
and else statements. Change
the opening brace to a colon
(:) and the closing brace to
endif; so that a HTML block
can be added to the if and
else part.
FINALS

Robot Fuel Checker -


ACTIVIT
Y
#1
TIME: 35mins
A robot travels a fixed distance of 250 units. It consumes fuel at a
rate of 7 units per 10 distance units.
Write a PHP script that:
● Defines how much fuel the robot has (e.g., $fuel = 180;)
● Calculates how far the robot can go with that fuel
● If the robot can finish the full trip (250 units), print "Mission
Complete!"
● Otherwise, print "Need more fuel."
Robot Fuel Checker - Example
Output
Pay attention to
Styling 😉
Mission Complete: Need More Fuel:
Super Globals
$_SERVER
The $_SERVER is a superglobal in PHP. It includes information
about HTTP headers, path, script location, and other things. It is an
associative array that contains information about the execution
environment and server.
The majority of these details are filled in by the web server and
each server may have different entries. When running PHP
scripts from the command line, some of these entries might not
be available.
$_SERVER VARIABLES
PHP_SELF - Stores filename of currently executing script.
SERVER_ADDR - This property of array returns the IP address of the
server under which the current script is executing.
SERVER_NAME - Name of server host under which the current script
is executing. In case of a server running locally, localhost is returned.
QUERY_STRING - A query string is the string of key value pairs
separated by the "&" symbol and appended to the URL after the "?"
symbol.
For example, http://localhost/testscript?name=xyz&age=20 URL
returns trailing query string
$_SERVER VARIABLES

REQUEST_METHOD - HTTP request method used for accessing a


URL, such as POST, GET, POST, PUT or DELETE.
In the above query string example, a URL attached to query string
with the "?" symbol requests the page with GET method

HTTPS - Set to 'on' if the request was made over HTTPS, otherwise
not set.
$_POST
The $_POST variable in PHP is a super global array that collects
form data after submitting an HTML form using the POST
method. It is particularly useful for securely sending data and
receiving user input
$_POST is a built-in PHP array. It stores data received from an
HTML form using the POST method.This data is not visible in the
URL, making it more secure than the GET method
$_GET
$_GET is one of the superglobals in PHP. It is an associative array of
variables passed to the current script via the query string appended to
the URL of HTTP request. Note that the array is populated by all
requests with a query string in addition to GET requests.
When someone visits a URL
like this −

PHP can access the values using $_GET.


FINALS

Mystery Locker -
ACTIVIT
Y
#2
TIME: 45mins
You are designing a simple security system for a mystery locker.
Users must enter a secret code (a number) into a form.
Your PHP script should:
● Accept the user's input using a form (via POST)
● If the code is exactly 7391, show:
"Access Granted. Locker Opened!"
● Otherwise, show:
"Access Denied. Intruder Alert!"
● Display a message only after the form is submitted (not on page
Mystery Locker - Example Output

Correct code: Incorrect code:

You might also like