Search for notes by fellow students, in your own course and all over the country.

Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.

My Basket

You have nothing in your shopping cart yet.

Title: PHP
Description: A complete basic tutorial of PHP the language that rules the internet.

Document Preview

Extracts from the notes are below, to see the PDF you'll receive please use the links above


PHP

• 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

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 is compatible with almost all servers used
today (Apache, IIS, etc
...
Download it from the official PHP
resource: www
...
net
• PHP is easy to learn and runs efficiently on the
server side

SYNTAX
• PHP script can be placed anywhere in the
document
...
php"
...





My first PHP page


echo "Hello World!";
?>




• Variables are "containers" for storing information
...

• When a variable is declared, it can be used over
and over again in your script
...

• The correct way of declaring a variable in PHP:
• $var_name = value;

Rules for PHP variables:
• A variable starts with the $ sign, followed by the
name of the variable
• A variable name must start with a letter or the
underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric
characters and underscores (A-z, 0-9, and _ )
• Variable names are case-sensitive ($age and
$AGE are two different variables)

• creating a variable containing a string, and a
variable containing a number:
$txt="Hello World!";
$x=16;
?>

Output Variables
• The PHP echo statement is often used to
output data to the screen
...
com";
echo "I love $txt!";
?>

In PHP there are two basic ways to get
output: echo and print
...
";
?>

$txt1 = "Learn PHP";
$txt2 = "W3Schools
...
com";
$x = 5;
$y = 4;
print "

"
...
"

";
print "Study PHP at "
...
"
";
print $x + $y;
?>




• The concatenation operator (
...

• To concatenate two string variables together,
use the concatenation operator:
$txt1="Hello World!";
$txt2="What a nice day!";
echo $txt1
...

• PHP automatically converts the variable to the
correct data type, depending on its value
...

• In PHP, the variable is declared automatically
when you use it
...

• Let's find the length of a string:
echo strlen("Hello world!");
?>
• The output of the code above will be:
• 12

• The PHP str_word_count() function counts the
number of words in a string:
• Example
echo str_word_count("Hello world!"); //
outputs 2
?>

• The PHP strrev() function reverses a string:
• Example
echo strrev("Hello world!");
• // outputs !dlrow olleH
?>

• The PHP str_replace() function replaces some
characters with some other characters in a string
...

• If a match is found, this function will return the position of
the first match
...

• Let's see if we can find the string "world" in our string:
echo strpos("Hello world!","world");
?>
• The output of the code above will be:
• 6
• The position of the string "world" in our string is position 6
...


• In PHP, variables can be declared anywhere in
the script
...

• PHP has three different variable scopes:
• local
• global
• static

Global and Local Scope
• A variable declared outside a function has a GLOBAL SCOPE
and can only be accessed outside a function:
• Example
$x = 5; // global scope

function myTest() {
// using x inside this function will generate an error
echo "

Variable x inside function is: $x

";
}
myTest();
echo "

Variable x outside function is: $x

";
?>

• A variable declared within a function has a LOCAL SCOPE
and can only be accessed within that function:
• Example
function myTest() {
$x = 5; // local scope
echo "

Variable x inside function is: $x

";
}
myTest();
// using x outside the function will generate an error
echo "

Variable x outside function is: $x

";
?>

PHP The global Keyword
• The global keyword is used to access a global variable from within a
function
...
However, sometimes we want a local variable NOT to be deleted
...

• To do this, use the static keyword when you first declare the variable:
• Example
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
myTest();
myTest();
?>

PHP Operators





Arithmetic Operators
Assignment Operators
Comparison Operators
Logical Operators

Conditional Statements
• In PHP we have the following conditional statements:
• if statement - use this statement to execute some code
only if a specified condition is true
• if
...
elseif
...


ARRRAYS
• An array stores multiple values in one single
variable:
• Example
$cars = array("Volvo", "BMW", "Toyota");
echo "I like "
...
$cars[1]
...
$cars[2]
...
$cars[0]
...
" and "
...
";
?>

• The count() function is used to return the
length (the number of elements) of an array:
• Example
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>

• To loop through and print all the values of an indexed
array, you could use a for loop, like this:
• Example
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);
for($x = 0; $x < $arrlength; $x++) {
echo $cars[$x];
echo "
";
}
?>

PHP Associative Arrays
• Associative arrays are arrays that use named keys
that you assign to them
...

• A function is a block of statements that can be used
repeatedly in a program
...

• A function will be executed by a call to the function
...

";
}
familyName("Jani");
familyName("Hege");
familyName("Stale");
familyName("Kai Jim");
familyName("Borge");
?>



function familyName($fname, $year) {
echo "$fname Refsnes
...
sum(5, 10)
...
sum(7, 13)
...
sum(2, 4);
?>

PHP 5 Form Handling
• simple HTML form with two input fields and a submit
button:
• html>



Welcome



Your email address is:



• The output could be something like this:
• Welcome John
Your email address is john
...
com

GET METHOD




Your email address is: $_GET["email"]; ?>




PHP COOKIES
• A cookie is often used to identify a user
...

• Each time the same computer requests a page
with a browser, it will send the cookie too
...


• PHP transparently supports HTTP cookies
...
For
example name, age, or identification number etc
...

• When next time browser sends any request to web
server then it sends those cookies information to the
server and server uses that information to identify the
user
...

• Syntax
• setcookie(name, value, expire, path, domain,
secure, httponly);
• Only the name parameter is required
...

• The setcookie() function must appear BEFORE
the tag
...

This variable is used while accessing cookies
...
$_COOKIE or $HTTP_COOKIE_VARS[]
• Value − This sets the value of the named variable and is
the content that you actually want to store
...
After this time cookie
will become inaccessible
...


• Path − This specifies the directories for which the
cookie is valid
...

• Domain − This can be used to specify the domain
name in very large domains and must contain at
least two periods to be valid
...

• Security − This can be set to 1 to specify that the
cookie should only be sent by secure
transmission using HTTPS otherwise set to 0
which mean cookie can be sent by regular HTTP
...

• setcookie("age", "36", time()+3600, "/", "", 0); ?>


Setting Cookies with PHP






Accessing Cookies with PHP

















Accessing Cookies with PHP


...
"
";
echo $_COOKIE["age"]
...
"
";
?>



You can use isset() function to check if
a cookie is set or not
...
$_COOKIE["name"]
...
Not recognized"
...
";
?>



$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30),
"/"); // 86400 = 1 day
?>


if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '"
...
"' is not set!";
} else {
echo "Cookie '"
...
"' is set!
";
echo "Value is: "
...
But there are differences for sure
...
Cookies are on
client side
...
For cookies, you can set time that when
it will be expired
...
Because, since
stored on client's computer, there are ways to
modify or manipulate cookies
Title: PHP
Description: A complete basic tutorial of PHP the language that rules the internet.