Intro To PHP
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.
• 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?
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
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 −
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