Lecture Silde 1-24
Lecture Silde 1-24
Server:
1. Homepage
lookup
2. Send as
HTTP
Response
HTTP Request: GETwww.xkcd.com
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_COOKIE
$_SESSION
PHP Global Variables - Superglobals
$GLOBALS: $GLOBALS is a PHP super global variable which is used
to access global variables from anywhere in the PHP script (also from
within functions or methods).
PHP stores all global variables in an array called $GLOBALS[index]. The
index holds the name of the variable.
Example:
<?php
$x = 75;
$y = 25; Output: 100
function addition() {
$GLOBALS['z'] = $GLOBALS['x']
+ $GLOBALS['y']; }
addition();
echo $z;
?>
PHP Global Variables - Superglobals
$_SERVER: $_SERVER is a PHP super global variable which holds
information about headers, paths, and script locations.
Some Example:
<?php
echo $_SERVER['PHP_SELF'];
Output:
echo "<br>"; /Practice/practice.php
echo $_SERVER['SERVER_NAME']; localhost
echo "<br>"; C:/xampp/htdocs/Practice/practice.php
echo $_SERVER['SCRIPT_FILENAME']; GET
echo "<br>";
echo $_SERVER['REQUEST_METHOD']; ?>
$_SERVER['PHP_SELF'] : Returns the filename of the currently executing script.
$_SERVER['SERVER_NAME'] : Returns the name of the host server.
$_SERVER['SCRIPT_FILENAME']: Returns the absolute pathname of the currently executing
script.
$_SERVER['REQUEST_METHOD']: Returns the request method used to access the page.
PHP Global Variables - Superglobals
$_REQUEST: $_REQUEST is a PHP super global variable which is
used to collect data after submitting an HTML form. Example
<html> <body>
<form method="post" action= " " >
Name: <input type="text" name="fname">
Form:
<input type="Submit"> Name: ICE Submit
</form> Output: ICE
<?php
if ($_SERVER["REQUEST_METHOD"] == When a user submits the data by
"POST") { clicking on "Submit", the form data is
// collect value of input field sent to the file specified in the action
$name = $_REQUEST['fname']; attribute of the <form> tag. If you
if (empty($name)) { wish to use another PHP file to
echo "Name is empty"; } process form data, replace that with
else { echo $name; } } ?> the filename of your choice.
</body> </html>
Operators
Arithmetic operators
+, -, *, /, % (modulus – remainder after division)
Logical AND (&&), OR (||), NOT (!)
Assignment operators
Shorthand for assignment operators:
$x += $y equivalent to $x = $x + $y
Also works with subtraction, multiplication, division, modulus, and string
concatenation
String operators:
Dot( .) operator, $x . $y Concatenation of $x and $y
Dot equal( .=) operator, $x . =$y Appends of $x and $y
Example: $a = ‘hello’; $b = ‘world !’;
// prints hello world !
Use of dot (.) Operator, echo $a.$b;
Use of dot equal(.=) Operator, $a.=$b echo $a;
== versus ===
Two “equality” operators
== tests for “equality” in value but not
necessarily type
=== tests for “identity” in value and type
== ignores the distinction between:
Integers, floating point numbers, and strings
containing the same numerical value
Nonzero numbers and boolean TRUE
Zero and boolean FALSE
Empty string, the string ‘0’ and boolean FALSE
Any other non-empty string and boolean TRUE
Strings
A sequence of characters
Single and double quotes:
Suppose $str = 42;
echo ‘With single quotes, str is $str’;
output: With single quotes, str is $str
echo “With double quotes, str is $str”;
output: With double quotes, str is 42
String Function
Length: strlen()
Position of substring: strpos()
Count Words in a String: str_word_count()
Reverse a String: strrev()
Replace Text Within a String: str_replace()
StringFunction Example