SERVER SIDE SCRIPTING BASIC-php
SERVER SIDE SCRIPTING BASIC-php
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)
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>
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.
For example:
$age = 21;
$price = 20.52;
$temperature = -5;
$name = “Clark Kent”;
echo “Your age is $age”;
PHP $ and $$ Variables
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:
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
<?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
?>
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.
Name Description
Represents full path and file name of the file. If it is used inside an
__FILE__
include, name of included file is returned.
Represents the name of the class method where it is used. The method
__METHOD__
name is returned as it was declared.
Question
Comment
Thank you!