0% found this document useful (0 votes)
37 views42 pages

SERVER SIDE SCRIPTING BASIC-php

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
37 views42 pages

SERVER SIDE SCRIPTING BASIC-php

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 42

Chapter One

SERVER SIDE SCRIPTING


BASIC

By:- Abreham G.
Introduction
 What is server side scripting language ?
a server-side scripting language is a script that is
executed on the server as apposed to client-side
scripting languages like JavaScript.
 It requires server-side scripting engine.
Cont’d.…
When to use server-side scripting?
 When developing Dynamic web pages.
 Authentication, authorization and session tracking.
 Template-driven page generation. Including
repeated content like header/footers and
navigation menus around the “content area” of a
web page.
What is PHP?
 PHP is a open source, interpreted and object-
oriented scripting language i.e. executed at server
side. It is used to develop web applications (an
application i.e. executed at server side and
generates dynamic page).
 PHP is an interpreted language, i.e. there is no need
for compilation.
 PHP is a server side scripting language.
 PHP is faster than other scripting language e.g. asp
and jsp.
Introduction to PHP
 PHP is the web development language written by and for
Web developers.
 PHP stands for Hypertext Preprocessor.
 PHP is currently in its Seventh major version called PHP7.
 PHP is a server-side scripting language, which can be
embedded in HTML or used as a standalone binary.
 Proprietary products in this niche are:
 Microsoft’s Active Server Pages (ASP)

 Macromedia’s ColdFusion, and

 Sun’s Java Server Pages (JSP).


Introduction to PHP…
 PHP is a server-side scripting language, which means that the scripts
are executed on the server, the computer where the Web site is located.
 PHP is different from JavaScript
 JavaScript is executed by the browser, on the user’s computer.
 Thus, JavaScript is a client-side language.
 Because PHP scripts execute on the server, PHP can dynamically
create the HTML code that generates the Web page.
 This allows individual users to see customized Web pages.
 Web page visitors see the output from scripts, but not the scripts
themselves.
Introduction to PHP…
 PHP is particularly strong in its ability to interact with databases.
 PHP supports pretty much every database you’ve ever heard of and
some you haven’t.
 PHP handles connecting to the database and communicating with
it, avoiding the knowledge of technical details for connecting to a
database.
 You tell PHP the name of the database and where it is, and PHP
handles the details.
 It connects to the database, passes your instructions to the database,
and returns the database response to you.
 Major databases currently supported by PHP include the following:
 dBASE  mSQL
 MySQL
 Informix
Oracle
 Ingres PostgreSQL
 Microsoft SQL Server Sybase
Introduction to PHP…
 In addition, PHP supports ODBC (Open Database
Connectivity).
 ODBC is a standard that allows you to communicate with
even more databases, such as Access and IBM DB2.
 PHP works well for a database-driven Web site.
 PHP scripts in the Web site can store data in and retrieve data
from any supported database.
 PHP also can interact with supported databases outside a Web
environment.
 Database use is one of PHP’s best features.
Software Requirements
 To work with PHP, you need to install a web server, PHP, and database server.
 The most commonly used web server is Apache(free)
 Similarly, for database, there are many options to use.
 The most popular database server for web pages is MySQL (free).
 PHP is also available freely on the internet.
 Software producers integrate the three software and offer them as bundle.
 Some of such bundles are:
 Vertrigo(Apache, PHP, Mysql)

 Wamp (Windows) (Apache, Mysql, PHP)

 Xampp(Apache, Mysql, PHP, Perl)

 Lamp (Linux)(Apache, PHP, Mysql)

 PHP files are executed through the web server only, not directly
http://127.0.0.1/test.php
Writing PHP
 You add PHP code to your web page by using tags, similar to other tags in
the HTML file.
 The PHP code section is enclosed in PHP tags with the following form:
<?php
PHP statements
?>

 For example, you can add the following PHP section to your HTML file.
<?php
echo “This line brought to you by PHP”;
?>
 Web pages that contains PHP should be saved with .php extension.
 You can also add several PHP sections to a Web page.
Writing PHP…
 There are actually four different styles of PHP tags we can use.
 Short style
<?
echo “<p>Order processed. </p>”;
?>
 This style of tag is the simplest and follows the style of an SGML.
 To use this tag, you either need to enable short tags in your config file
(short_open_tag = On), or compile PHP with short tags enabled.
 XML style
<?php
echo “<p>Order processed. </p>”;
?>
 This style of tag can be used with XML documents.
 Most commonly used tag in literatures
Writing PHP…
 SCRIPT style
<SCRIPT LANGUAGE=”php”>
echo “<p>Order processed. </p>”;
</SCRIPT>
 This style of tag is the longest and will be familiar if you’ve used
JavaScript.
 ASP style
<%
echo “<p>Order processed. </p>”;
%>
 This style of tag is the same as used in Active Server Pages (ASP).
 It can be used if you have enabled the asp_tags configuration
setting.
Writing PHP…
 Example: your first hello world PHP script
<html>
<head><title>Hello World Script</title></head>
<body>
<?php
echo “<p>Hello World!</p>”
?>
</body>
</html>

 When the PHP section is processed, it is replaced with the output.


 In this case, the output is as follows:
<p>Hello World!</p>
Attaching PHP to Web Pages
 PHP file is attached to web pages by using the <form> tag.
 Since PHP is written to process data sent from client computers and form is the
means to get that data, this is a logical place to attach the PHP to HTML.
 We use the action attribute to specify the PHP to execute when the web page is
submitted.
<form name=”formname” method=”submitingmethod” action=”phpfile.php”>
form elements
</form>
 Example: to attach a php called saver.php to an HTML page
<form name=”detail” method=”post” action=”saver.php”>
form content
</form>
 When this form is submitted, the web server looks for saver.php and executes it.
 The result from executed php is sent back to the client machine.
Output Statements
 The two most basic ways for displaying output in PHP are echo and
print.
 Both can be used either with parentheses or without them.
 Function calls always have the name of the function first, followed by
a parenthesized list of the arguments to the function.
 The general format of the echo statement is as follows:
echo outputitem1, outputitem2, outputitem3, . . .;
echo (output);
 The parameterized version of echo does not accept multiple
arguments.
 The general format of the print statement is as follows:
print output;
print(output);
Output Statements…
 Example: different ways of echo and print
echo 123; //output: 123
echo “Hello World!”; //output: Hello world!
echo (“Hello World!”); //output: Hello world!
echo “Hello”,”World!”; //output: Hello World!
echo Hello World!; //error, string should be in quotes
print (“Hello world!”); //output: Hello world!
print “Hello world!”; //output: Hello world!
 The command print is very similar to echo, with two important differences:
 Unlike echo, print can accept only one argument.

 Unlike echo, print returns a value, which represents whether the print statement
succeeded.
Output Statements…
 The value returned by print will be 1 if the printing was
successful and 0 if unsuccessful.
 It is rare that a syntactically correct print statement will fail.
 But in theory this return value provides a means to test, for
example, if the user’s browser has closed the connection.

 It is possible to embed HTML tags in echo or print


statements.
 The browser will parse and interpret them like any tag
included in HTML page and display the page accordingly.
Comments
 PHP supports two types of comments:
 single-line comment (short comment), and
 multi-line comment (long comment).

 The format for multi-line comments is as follows:


/* comment text
more comment text
*/

 Your comments can be as long or as short as you need.


Comments…
 PHP has a short comment format too.
 You can specify a single line comment by using the # or //
# This is comment line 1
// This is comment line 2
 It is customary and useful to put a block of comments at the top of your script giving
information about the script and an overview of what it does.
/*
name: hello.php
description: Displays “Hello World!” on a Web page.
written by: John Steve
created: 2/1/11
modified: 3/15/11
*/
PHP Variables
 In PHP, all variable names should start with a dollar sign ($).
 This tells PHP that it is a variable name.
 To store information in a variable you use a single equal sign
(=).

 For example:
$age = 21;
$price = 20.52;
$temperature = -5;
$name = “Clark Kent”;
echo “Your age is $age”;
PHP $ and $$ Variables

 The $var (single dollar) is a normal variable with the name


var that stores any value like string, integer, float, etc.
 The $$var (double dollar) is a reference variable that stores
the value of the $variable inside it.
 <?php
 $x = "abc";
 $$x = 110;
 echo $x."<br/>"; // it prints abc
 echo $$x."<br/>"; // it printts 110
 echo $abc; // it printts 110
 ?>
Data type in PHP
 PHP has the following data types: integers, doubles, Booleans,
strings, arrays, objects, and resources:
 Integers are whole numbers, without a decimal point, like 495.
 Doubles are floating-point numbers, like 3.14159 or 49.0.
 Booleans have only two possible values: TRUE and FALSE.
 Strings are sequences of characters, like “PHP is very interesting”.
 Arrays are named and indexed collections of other values.
 Objects are instances of programmer-defined classes.
 Resources are special variables that hold references to resources
external to PHP such as database connections.
Cont’d
 PHP is a loosely typed language.
 This means that a single variable may contain any type of
data.
 This could be a number, a string of text, or some other kind
of value, and may change types over its lifetime.

 Example:
$testvar = 3 + 4;
echo “The value is $testvar”; //output: The value is 7
$testvar = “three”;
echo “The value is $testvar”; //output: The value is three
Display &Remove Variables
Displaying variable values
 You can display the value stored in a variable with print statement. You can output
the value of a variable as in the following statements:
$today = “Sunday”;
print(“The day today is $today”);
 The output from the preceding statements is “The day today is Sunday”.
Removing Variables
 You can uncreate the variable by using this statement:
unset($age);
 After this statement, the variable $age no longer exists.
 If you try to echo it, you get an “undefined variable” notice.
 You can unset more than one variable at once, as follows:
unset($age, $name, $address);
PHP variable checking
Checking variable content
 Sometimes you just need to know whether a variable exists or what
type of data is in the variable.
 Here are some common ways to test variables:

 isset($varname) - true if variable is set, even if nothing is stored in


it.
 empty($varname) - true if value is 0 or is a string with no characters
in it or is not set.
 You can also test what type of data is in the variable.

 For example, to see if the value is an integer:

 is_int($number) - the comparison is TRUE if the value in $number


is an integer.
PHP variable checking
 Some other tests provided by PHP are as follows:
 is_integer($var): same as is_int($var)
 is_array($var2): Checks to see if $var2 is an array
 is_float($number): Checks if $number is a floating point number
 is_numeric($string): Checks to see if $string is a numeric string
 is_string($string): Checks to see if $string is a string
 is_bool($var): finds out whether a variable is a boolean

 You can test for a negative, as well, by using not operator (!) in
front of the expression.
 For example, the following statement returns TRUE if the
variable does not exist at all:
 !isset($varname)
Cont’d…
<?php
$a = "test";
$b = "anothertest";
echo isset($a); // TRUE
echo isset($a, $b); //TRUE
unset ($a);
echo isset($a, $b); //FALSE
$var = 0;
if (empty($var)) // evaluates TRUE
echo '$var is either 0 or not set at all';
?>
PHP type casting
Type Casting
 We often work with multiple data types at once.
 Converting one data type to another one is a common task in
programming.
 Type conversion or typecasting refers to changing an variable of one data
type into another.
 There are two types of conversion.
 Implicit and

 Explicit

 Implicit type conversion is an automatic type conversion by the compiler.


 Example:
echo "45" + 12; //output: 57
echo 12 + 12.4; //output: 24.4
Cont’d…
 Explicit conversion happens, when we use the cast
constructs.
 There are two ways to do this:
 Using bool settype ( mixed var, string type) function

 Using cast methods

Let us see bool settype :


 Settype sets the type of a variable.
 The syntax is:
bool settype ( mixed var, string type)
Cont’d…
Example:
$foo = "321.456number";
settype($foo, "float");
print("<br>Float: $foo"); //output: Float: 321.456
$foo = "321.456number";
settype($foo, "integer");
print("<br>Integer: $foo"); //output: Integer: 321
$foo = 321.456;
settype($foo, "string");
print("<br>String: $foo"); //output: String: 321.456
echo gettype("4"); Output: String
Using cast methods
$ab="123n";
$ab=(int)$ab;
echo $ab; //output integer 123
PHP Strings
 A string is a sequence of characters, like “Abebe Beso
Bella ! Lemin Bela ? No answer ".
 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!":
 <?php
echo strlen("Hello world!"); // outputs 12
?>
Cont’d…
 Count The Number of Words in a String
 The PHP str_word_count() function counts the number of
words in a string:
<?php
echo str_word_count("Hello world!"); // outputs 2
?>
 Reverse a String

 The PHP strrev() function reverses a string:

<?php
echo strrev("Hello world!"); // outputs !dlrow olleH
?>
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
?>

 Tip: The first character position in a string is 0 (not 1).


 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!
?>
 Reading assignment other string functions
PHP Constants
Creating Constants
 We use constants to define values that do not change like PI in
maths(3.14..), number of days in a week(7), number of minutes in an
hour(60), etc.
 Constants are set by using the define statement.

 The general format is as follows:

define(“constantname”, ”constantvalue”);
 For example, to set a constant with the weather, use the following
statement:
define(“PI”,”3.141”);
 This statement creates a constant called PI and sets its value to

“3.141”.
Cont’d…
Creating Constants
PHP constant: const keyword
The const keyword defines constants at compile time. It is a language
construct not a function.
It is bit faster than define().
It is always case sensitive.

<?php
const MESSAGE="Hello constantS";
echo MESSAGE;
?>
Cont’d…
 unlike variables, constant names do not begin with a dollar sign ($).
 By convention, constants are given names that are all uppercase
 so you can see easily that they’re constants.
 However, PHP accepts lowercase letters without complaint.
 You can store either a string or a number in a constant.
 Example:
define (“INTEREST”,0.01);
 Constants should not be given names that are keywords for PHP.
 Keywords are words that have meaning for PHP, such as echo, and
they can’t be used as constants because PHP treats them as the PHP
feature of the same name.
Cont’d…
Displaying constants
 You can determine the value of a constant by using print as
follows:
print(INTEREST);
 You can also use a constant in an echo statement:

echo INTEREST;
 When you echo a constant, you can’t enclose it in quotes.

 If you do, it echoes the constant name rather than the value.

 You can build more complicated output statements by using


commas, as in the following example:
echo “The Canadian exchange rate is $”, INTEREST;
Magic Constants

Name Description

__LINE__ Represents current line number where it is used.

Represents full path and file name of the file. If it is used inside an
__FILE__
include, name of included file is returned.

Represents full directory path of the file. Equivalent to


__DIR__ dirname(__file__). It does not have a trailing slash unless it is a root
directory. It also resolves symbolic link.

Represents the function name where it is used. If it is used outside of


__FUNCTION__
any function, then it will return blank.

Represents the class name where it is used. If it is used outside of any


__CLASS__
class, then it will return blank.

Represents the trait name where it is used. If it is used outside of any


__TRAIT__ function, then it will return blank. It includes namespace it was declared
in.

Represents the name of the class method where it is used. The method
__METHOD__
name is returned as it was declared.

__NAMESPACE__ Represents the name of the current namespace.


 Be the digger of

Live For What You Believe!!!!!!!


End of Chapter One

Question
Comment

Thank you!

You might also like