Chapter 1 IP

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 18

Prepared By: Mr. Jay A.

Abaleta 1

CHAPTER 1
The Server Side Scripting Basics

erver-side scripting
is a web server technology in which a user's request
is fulfilled by running a script directly on the web
server to generate dynamic web pages.

It is usually used to provide interactive web sites


that interface to databases or other data stores.

This is different from client-side scripting where


scripts are run by the viewing web browser
Primary advantage to server-side scripting
2
1. The ability to highly customize the response based
on the user's requirements

2. Access rights

3. Queries into data stores.

About security:
Server-side scripts are never visible to the
browser as these scripts are executes on
the server and emit HTML corresponding
to user's input to the page.

Prepared By: Mr. Jay A. Abaleta


What is PHP?
3
- PHP Originally stood for “PERSONAL HOME PAGE”
- Created in 1994 by Rasmus Lerdorf
- New term of PHP – “Hypertext Preprocessor”
- Is a server-side scripting language, like ASP

- PHP scripts are executed on the server

- It supports databases (MYSQL,ORACLE,Generic ODBC, etc.)

- It is an Open Source Software (OSS)

- It is free to download and use

Prepared By: Mr. Jay A. Abaleta


Basic Syntax of PHP
4
<?php (the PHP start tag)
statement;
?> (the PHP end tag).

Example:
<html> Output:
<head><title>PHP says hello</title></head>
<body>
<b>
<?php
print "Hello, World!";
?>
</b>
</body>
</html>

Prepared By: Mr. Jay A. Abaleta


Calling echo and print in PHP Script

5 <html>
<head>
<title>Hello World</title>
</head>
<body>
<?php
echo ("Hello world!<br />");
print ('Goodbye.<br />');
print 'Over and out.';
?>
</body> Output:
</html>
Hello world!
Goodbye.
Over and out.

Prepared By: Mr. Jay A. Abaleta


Sending Data to the Web Browser
6
send.php
<html>
<head> sayhello.php
<title>PHP says hello</title>
</head> <?php
<body> print $_POST['user'];
<form method="POST" action="sayhello.php"> print ‘Hello, ‘;
Your Name: <input type="text" name="user"> print ‘!’;
<br/>
<input type="submit" value="Say Hello">
?>
</form>
</body>
</html>

Output #1 Output #2

Prepared By: Mr. Jay A. Abaleta


PHP Comments
2- comment styles in PHP
7 1. // - for single-line comments
2. /* …. */ - for Multi-line comments
Sample Comments Statement:
<html>
<head>
<title>Hello World</title>
</head>
<body>
<?php

// A single line comment could say that we are going to


// print hello world.

/* This is how to do a
multi-line comment and could be used comment out a block
of code */

echo("Hello world!<br />");


print('Goodbye.<br />');

?>
</body>
</html>

Prepared By: Mr. Jay A. Abaleta


Variables in PHP
8
All variables in PHP start with a $ sign symbol. Variables may
contain strings, numbers, or arrays.
In PHP, you define a variable with the following form:

$variable_name = value;

Rules for Declaring Variables in PHP:


•PHP variables may only be composed of alphanumeric characters and underscores;
for example, a-z, A-Z, 0-9, and _.

•Variables in PHP are case sensitive. This means that $variable_name and $Variable_Name
are different.

•Variables with more than one word should be separated with underscores; for
example, $test_variable.

•Variables can be assigned values by using the equals sign (=).

•Always end with a semicolon (;) to complete the assignment of the variable.

Prepared By: Mr. Jay A. Abaleta


To create a simple PHP variable:
9
<?php
$age = 30;
?>

Example, Reassigning a variable


<?php
$age = 30;
echo $age;
?>

Output: 30

Prepared By: Mr. Jay A. Abaleta


PHP super globals
Variable array name Contents
10
Contains any global variables that are accessible for the local script.
$GLOBALS The variable names are used to select which part of the array to
access.

$_SERVER Contains information about the web server environment.

$_GET Contains information from GET requests (a form submission).

Contains information from POST requests (another type of form


$_POST
submission).
$_COOKIE Contains inform from HTTP cookies.
$_FILES Contains information from POST file uploads.

$_ENV Contains information about the environment (Windows or Mac).

Contains information from user inputs. These values should not be


$_REQUEST
trusted.

$_SESSION Contains information from any variables registered in a session.

Prepared By: Mr. Jay A. Abaleta


Working and Manipulating Strings in PHP
11 - string can be used directly in a function call or it can be stored in a
variable.

Example, Working with strings:

<?php
$my_string = "Jay is Handsome!";
echo " Jay is Handsome!";
?>

Example, Using a variable in a string defenition

<?php
$my_string = "Jay is Handsome!";
echo “Time for Saying $my_string";
?>

Prepared By: Mr. Jay A. Abaleta


Comparing strings
PHP has functions to compare strings that aren't exactly alike. For example,
12
you may want to consider "Bill" to be the same as "BILL," ignoring the case
of the string.

Example. Using strcasecmp to compare two strings

<?php
$name1 = "Bill";
$name2 = "BILL";
$result = strcasecmp($name1, $name2);
if (!$result)
{
echo "They match.";
}
?>

Prepared By: Mr. Jay A. Abaleta


Concatenation
-Concatenation combines one or more text strings and variables
13
Example. Concatenating strings together
<?php
$my_string = "Hello Jay. My name is: ";
$newline = "<br />";
echo $my_string . "Paula" . $newline;
echo "Hi, I'm Jay. Who are you? " . $my_string . $newline;
echo "Hi, I'm Jay. Who are you? " . $my_string . "Paula";
//The last line is the same as echo "Hi, I'm Jay. Who are you? $my_string Paula";
?>

Output:
Hello Jay. My name is: Paula
Hi, I'm Jay. Who are you? Hello Jay. My name is:
Hi, I'm Jay. Who are you? Hello Jay. My name is: Paula

Prepared By: Mr. Jay A. Abaleta


Working and Manipulating Numbers in PHP
14
Doing Math:
Variables can hold numbers, too, and it's useful to perform
mathematical operations on those numbers.

The basic mathematical operators

Mathematical operator Name Example Result

+ Addition 2+2 4

- Subtraction 2-1 1

* Multiplication 2*2 4

/ Division 2/2 1

% Modulo (remainder)

Prepared By: Mr. Jay A. Abaleta


Combined assignment
Combined assignment operators provide a shortcut for performing two
15
common tasks at the same time.

Combined assignment operators

Combined operation Operation Produces

$num+=y Addition $num=$num+y

$num -=y Subtraction $num=$num-y

$num *=y Multiplication $num=$num*y

$num /=y Division $num=$num/y

$num.= "y" Concatenation $string=$string."y"

Prepared By: Mr. Jay A. Abaleta


Autoincrement and autodecrement
It's very common when writing your code to ether increment or decrement a
16 variable by one.

Example: Increment

<?php
$counter=9;
$counter++;
echo "$counter";
?>

Example: decrement

<?php
$counter=9;
$counter--;
echo "$counter";
?>

Prepared By: Mr. Jay A. Abaleta


Work With Constant

17
- You can define constants in your program. A constant, like its
name implies, cannot change its value during the execution of your
program.

- It's defined using the define function, which takes the name of the
constant as the first parameter and the values as the second parameter

These are the differences between constants and variables:

•Constants do not have a dollar sign ($) before them.


•Constants can only be defined using the define function, not by simple
assignment.
•Constants are defined and accessed globally.
•Constants cannot be redefined or undefined once they have been set.
•Constants can only evaluate to scalar values.

Prepared By: Mr. Jay A. Abaleta


Example, Using a constant in your program

<?php
18
define("HELLO", "Hello world!");
echo HELLO; // outputs "Hello world."
?>

Sample Output:

Hello world!

Prepared By: Mr. Jay A. Abaleta

You might also like