0% found this document useful (0 votes)
16 views38 pages

PHP Chap1

The document provides an introduction to server-side scripting, explaining the differences between client-side and server-side scripting, including their advantages and limitations. It covers PHP as a server-side scripting language, detailing its syntax, variables, data types, and functions for handling strings and arrays. Additionally, it discusses the use of comments, constants, and the importance of case sensitivity in PHP programming.

Uploaded by

Melak Enchalew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views38 pages

PHP Chap1

The document provides an introduction to server-side scripting, explaining the differences between client-side and server-side scripting, including their advantages and limitations. It covers PHP as a server-side scripting language, detailing its syntax, variables, data types, and functions for handling strings and arrays. Additionally, it discusses the use of comments, constants, and the importance of case sensitivity in PHP programming.

Uploaded by

Melak Enchalew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

Chapter one

Introduction to server side scripting

Melak E.
How Web works?
 WWW use classical client / server architecture
 HTTP is text-based request-response protocol
Client-side scripting and Server-side scripting

 Client-side scripting: performed to generate a code


that can run on the client end (browser) without
needing the server side processing.
 Basically, these types of scripts are placed inside an
HTML document.
 The client-side scripting can be used to examine the
user’s form for the errors before submitting it and for
changing the content according to the user input.
Cont’d…
 It is designed to run as a scripting language utilizing a
web browser as a host program.
 For example, when a user makes a request via browser
for a webpage to the server, it just sent the HTML and
CSS as plain text, and the browser interprets and
renders the web content in the client end.
 Examples:
 HTML
 CSS
 Java script
 Java Applets
 Bootstrap etc
Advantages of client-side scripting

 They can be used to validate user input


 To interact with the browser
 To enhance web pages
 Add client/server communication between a browser and a
web server
 Limitations
 Browser dependency
 The browser or scripting host must support the scripting language and
capabilities.
 Cannot connect to a back-end server, such as a data base or file handling
systems.
Server-side scripting
 Server-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.
 The operations like customization of a website, dynamic
change in the website content, response generation to the
user’s queries, accessing the database, and so on are
performed at the server end.
 It is usually used to provide interactive web sites that interface
to databases or other data stores.
 Examples:
 PHP , ASP , JSP (java server page),ColdFusion, etc.
Cont’d…
 Advantages:
 The primary advantage to server-side scripting is the ability
to highly customize the response based on the user’s
requirements, access rights or queries into data stores.
 Session management which helps to maintain user identity
 Program flexibility
 Feasibility
 Browser independent
 Security
 Limitations:
 They can be slow due to server programs
 Reduce web page appearance, compared to client side scripting.
Client-side vs Server-side scripting

Client-side scripting Server-side scripting


 Source code is visible to user.  Code is not visible to user.
 Runs on users computer  Runs on web server
 Used for validations and  Used for business logic and
functionalities for user events. data access from database.
 Depends on the browser and  Not dependent on client.
version  It provides more security for
 It does not provide security data.
for data.
PHP- Introduction
 PHP stands for hypertext Preprocessor
 PHP is a programming language designed to generate web pages
interactively on the computer serving them, called a web server.
 Unlike HTML, where the web browser uses tags and markup to
generate a page, PHP code runs between the requested page
and the web server, adding to and changing the basic HTML
output.
 PHP supports many databases (MySQL, Informix, Oracle, Sybase,
Solid, PostgreSQL, Generic ODBC, etc.)
 PHP runs on different platforms (Windows, Linux, Unix, etc.).
 PHP is compatible with almost all servers used today (Apache,
IIS, etc.)
PHP Syntax
 PHP code is executed on the server, and the plain HTML
result is sent to the browser.
 A PHP scripting block always start with <?php and End
with ?>.
<?php
…….
?>
 Php scripting block placed any where in the document.
 Php short-open tags look like this:
<? ….. ?>
Cont’d…
 Below, we have an example of a simple PHP script which sends the text
"Hello World" to the browser:
<html>
<body>
<?php
echo “Hello World”;
?>
</body>
</html>
 Echo used to output data to the screen.
 PHP file must save using .PHP extension
 Each code line in PHP must end with a semicolon.
 A PHP file normally contains HTML tags, and some PHP scripting code.
Comments in PHP

 A comment in PHP code is a line that is not read/executed as


part of the program.
 Its only purpose is to be read by someone who is looking at
the code.
 Comments can be used to:
 Let others understand what you are doing
 Remind yourself of what you did - Most programmers have
experienced coming back to their own work a year or two
later and having to re-figure out what they did.
 Comments can remind you of what you were thinking when
you wrote the code.
Cont’d…
 In PHP, we use // to make a single-line comment or /* and */ to
make a large comment block.
 Example
<?php
// This is a single-line comment
# This is also a single-line comment
/*
This is a multiple-lines comment block
that spans over multiple
lines
*/
// You can also use comments to leave out parts of a code line
$x = 5 /* + 15 */ + 5;
echo $x;
?>
PHP variables
 Variables are used for storing values, such as numbers, strings
or arrays, so that they can be used many times in a script.
 All variables in PHP are denoted with a leading dollar sign ($).
 Creating (Declaring) PHP Variables
$var_name=value;
 A variable name can only contain alpha-numeric characters,
underscores (a-z, A-Z, 0-9, and _ )
 Example
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
Cont’d…
 Loosely Typed Language
 In PHP, a variable does not need to be declared before adding
a value to it.
 In the example above, you see that you do not have to tell
PHP which data type the variable is.
 PHP automatically converts the variable to the correct data
type, depending on its value.
 In a strongly typed programming language, you have to
declare (define) the type and name of the variable before
using it.
 In PHP, the variable is declared automatically when you use it.
Cont’d…
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)
 Example
<?php
$txt = “PHP";
echo “I love” .$txt!;
?>
PHP Case Sensitivity
 In PHP, all keywords (e.g. if, else, while, echo, etc.), classes,
functions, and user-defined functions are NOT case-sensitive.
 In the example below, all three echo statements below are legal
(and equal):
 Example
<?php
ECHO "Hello World!<br>";
echo "Hello World!<br>";
EcHo "Hello World!<br>";
?>
Cont’d…
 However, all variable names are case-sensitive.
 In the example below, only the first statement will display the
value of the $color variable (this is because $color, $COLOR,
and $coLOR are treated as three different variables):
 Example
<?php
$color = "red";
echo "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
echo "My boat is " . $coLOR . "<br>";
?>
PHP echo and print Statements
 In PHP there are two basic ways to get output: echo and
print.
 echo and print are more or less the same.
 They are both used to output data to the screen.
 echo has no return value while print has a return value of 1
so it can be used in expressions.
 echo can take multiple parameters (although such usage is
rare) while print can take one argument.
 echo is marginally faster than print.
Cont’d…
 The PHP echo Statement
 The echo statement can be used with or without parentheses: echo
or echo().
 Display Text
 The following example shows how to output text with the echo
command (notice that the text can contain HTML markup):
<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?>
Cont’d..
Displaying Variables
 The following example shows how to output text and
variables with the echo statement:
<?php
$txt1 = "Learn PHP";
$txt2 = “at UoG";
$x = 5;
$y = 4;

echo "<h2>" . $txt1 . "</h2>";


echo "Study PHP at " . $txt2 . "<br>";
echo $x + $y;
?>
Cont’d…
The PHP print Statement
 The print statement can be used with or without parentheses:
print or print().
Displaying text
 The following example shows how to output text with the print
command (notice that the text can contain HTML markup):
<?php
print "<h2>PHP is Fun!</h2>";
print "Hello world!<br>";
print "I'm about to learn PHP!";
?>
PHP Data types

 Variables can store data of different types, and different data


types can do different things.
 PHP supports the following data types:
 String
 Integer
 Float (floating point numbers - also called double)
 Boolean
 Array
 NULL
PHP String Variables

 A string variable is used to store and manipulate text.


 A string can be used directly in a function or it can be stored
in a variable.
 Below, the PHP script assigns the text "Hello World" to a
string variable called $txt:
<?php
$txt="Hello World";
echo $txt;
?>
Cont’d…
integers
 are whole numbers, without a decimal point, like 4195.
Rules for integers:
 An integer must have at least one digit
 An integer must not have a decimal point
 An integer can be either positive or negative
 Integers can be specified in three formats: decimal (10-based),
hexadecimal (16-based - prefixed with 0x) or octal (8-based -
prefixed with 0)
 In the following example $x is an integer.
Cont’d…
Example
 <?php
$x = 5985;
var_dump($x);
?>
PHP Float
 A float (floating point number) is a number with a decimal point or
a number in exponential form.
 In the following example $x is a float.
 The PHP var_dump() function returns the data type and value:
<?php
$x = 10.365;
var_dump($x);
?>
Cont’d…

PHP Boolean
 A Boolean represents two possible states: TRUE or
FALSE.
$x = true;
$y = false;
 Booleans are often used in conditional testing.
Cont’d…
Search for a specific text within a string
 The PHP strpos() function searches for a specific text within a
string.
 If a match is found, the function returns the character position
of the first match.
 If no match is found, it will return FALSE.
 The example below searches for the text "world" in the string
"Hello world!":
Example
<?php
echo strpos("Hello world!", "world"); // outputs 6
?>
Cont’d…
PHP Array
 An array stores multiple values in one single variable.
 In the following example $cars is an array.
 The PHP var_dump() function returns the data type and value:
 Example
<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
PHP object
 An object is a data type which stores data and information on how to process
that data.
 In PHP, an object must be explicitly declared.
 First we must declare a class of object. For this, we use the class keyword.
 A class is a structure that can contain properties and methods:
 Example
<?php
class Car {
function Car() {
$this->model = "VW";
}
}
// create an object
$herbie = new Car();
// show object properties
echo $herbie->model;
?>
Cont’d…
PHP NULL Value
 Null is a special data type which can have only one value:
NULL.
 A variable of data type NULL is a variable that has no value
assigned to it.
 Tip: If a variable is created without a value, it is automatically
assigned a value of NULL.
 Variables can also be emptied by setting the value to NULL:
Example
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
Cont’d…
Get The Length of a String
 The PHP strlen() function returns the length of a string.
 The example below returns the length of the string "Hello
world!":
 Example
 <?php
echo strlen("Hello world!"); // outputs 12
?>
Count The Number of Words in a String
The PHP str_word_count() function counts the number of
words in a string:
Cont’d…
Example
<?php
echo str_word_count("Hello world!"); // outputs 2
?>
Reverse a String
The PHP strrev() function reverses a string:
 Example
<?php
echo strrev("Hello world!"); // outputs !dlrow olleH
?>
Cont’d…
Replace Text Within a String
 The PHP str_replace() function replaces some characters
with some other characters in a string.
 The example below replaces the text "world" with "Dolly":
Example
<?php
echo str_replace("world", "Dolly", "Hello world!"); // outputs
Hello Dolly!
?>
Create a PHP Constant
 To create a constant, use the define() function.
 Syntax
define(name, value, case-insensitive)
 Parameters:
 name: Specifies the name of the constant
 value: Specifies the value of the constant
 Case-insensitive: Specifies whether the constant name should
be case-insensitive. Default is false
 The example below creates a constant with a case-sensitive
name:
Cont’d…
Example
<?php
define("GREETING", "Welcome to PHP!");
echo GREETING;
?>
 The example below creates a constant with a case-
insensitive name:
 Example
<?php
define("GREETING", “PHP!", true);
echo greeting;
?>
Cont’d…
 A constant is a name or an identifier for a simple value.
 A constant value cannot change during the execution of the
script.
 To define a constant you have to use define() function and to
retrieve the value of a constant,
 You can also use the function constant() to read a constant's
value if you wish to obtain the constant's name dynamically
<?php
define("MINSIZE", 50);
echo MINSIZE;
echo constant("MINSIZE");
?>
End of chapter 1

You might also like