0% found this document useful (0 votes)
68 views

WBP Topic 1 notes

Uploaded by

namdev misal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

WBP Topic 1 notes

Uploaded by

namdev misal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Topic 1

Expressions and control statements in PHP


1.1 History and Advantages of PHP-Syntax of PHP
History of PHP
 PHP began life as a simple way to track visitors to Rasmus Lerdorf's online resume. It
also could embed SQL queries in Web pages. But as often happens on the Web, admirers
quickly asked for their own copies. As a proponent of the Internet's ethic of sharing, as
well as a generally agreeable person, Rasmus unleashed upon an unsuspecting Web his
Personal Home Page Tools version 1.0.
 "Unleashed upon himself" may be more accurate. PHP became very popular. A
consequence was a flood of suggestions. PHP 1.0 filtered input, replacing simple
commands for HTML. As its popularity grew, people wondered if it couldn't do more.
Loops, conditionals, rich data structures—all the conveniences of modern structured
programming seemed like a next logical step. Rasmus studied language parsers, read
about YACC and GNU Bison, and created PHP 2.0’ PHP 2.0 allowed developers to
embed structured code inside HTML tags. PHP scripts could parse data submitted by
HTML forms, communicate with databases, and make complex calculations on the fly.
And it was very fast, because the freely available source code compiled into the Apache
Web server. A PHP script executed as part of the Web server process and required no
forking, often a criticism of Common Gateway Interface (CGI) scripts

Advantages of PHP
1. Cross-Platform

PHP is, an application can be run on various platforms. The most important advantage of PHP is
the developer need not have to worry about the operating system the user is working on. As the
PHP code run properly and smoothly on all operating systems. Furthermore, the server-side
scripting language or PHP hosting service providers can easily be found.

2. Ease of use

Any individuals who are new to programming can easily learn to use them within a short
duration of time. The syntax for programming in the hypertext preprocessor is quite similar to
the C programming language. So the individual who is familiar with C can easily create PHP
website scripts.

3. Speed
A very vital side of web development is the speed. Considering the very important fact, some
individuals still struggle with the challenge of web data speed. A quick loading website always
appreciated by many individuals. Furthermore, individuals typically like quick loading web sites
compared to slower loading ones. Folks use PHP because of the important fact that it’s quick.

4. Open source and Powerful library support

Another advantage to using the PHP hyper text Preprocessor is it’s developed and maintained by
a cluster of PHP developers, this help in making a support community, extensive extension
library. PHP additionally has immense collection functional modules and a few of the modules
available in PHP include Graphics and PDF amongst others.

5. Stable

PHP has been in existence for about twenty-two years. Over this era, many developers have
worked on the application to enhance the usage of the application. lots of bugs are discovered
over the years and therefore the bugs are fixed quickly by the team of developers. Based on this,
the programming language is currently very stable.

Syntax of PHP
A PHP script can be placed anywhere in the document.

A PHP script starts with <?php and ends with ?>:

<?php
// PHP code goes here
?>

The default file extension for PHP files is ".php".

A PHP file normally contains HTML tags, and some PHP scripting code.

Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP function
"echo" to output the text "Hello World!" on a web page:

Example
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>

<?php
echo "Hello World!";
?>

</body>
</html>

1.2 Variables, Data types, Expressions and operators, Constants


 Variables in PHP are identifiers prefixed with a dollar sign ($). For example:

$name
$Age
$_debugging
$MAXIMUM_IMPACT

 A variable may hold a value of any type. There is no compile-time or runtime type
checking on variables. You can replace a variable’s value with another of a different
type:

$what = "Fred";
$what = 35;
$what = array("Fred", 35, "Wilma");

 There is no explicit syntax for declaring variables in PHP. The first time the value of
a variable is set, the variable is created. In other words, setting a value to a variable
also functions as a declaration. For example, this is a valid complete PHP program:
$day = 60 * 60 * 24;
echo "There are {$day} seconds in a day.\n";
There are 86400 seconds in a day.
 A variable whose value has not been set behaves like the NULL value:

if ($uninitializedVariable === NULL) {


echo "Yes!";
}
Yes!

Variable Scope
The scope of a variable, which is controlled by the location of the variable’s declaration,
determines those parts of the program that can access it. There are four types of variable
scope in PHP: local, global, static, and function parameters.

Local scope
A variable declared in a function is local to that function. That is, it is visible only to code in that
function (including nested function definitions); it is not accessible outside the function. In
addition, by default, variables defined outside a function (called global variables) are not
accessible inside the function. For example, here’s a function that updates a local variable instead
of a global variable:

function updateCounter()
{
$counter++;
}
$counter = 10;
updateCounter();
echo $counter;
10

The $counter inside the function is local to that function, because we haven’t said otherwise. The
function increments its private $counter variable, which is destroyed when the subroutine ends.
The global $counter remains set at 10.
Only functions can provide local scope. Unlike in other languages, in PHP you can’t
create a variable whose scope is a loop, conditional branch, or other type of block.

Global scope
 Variables declared outside a function are global. That is, they can be accessed from
any part of the program. However, by default, they are not available inside
functions. To allow a function to access a global variable, you can use the global
keyword inside the function to declare the variable within the function. Here’s how
we can rewrite the updateCounter() function to allow it to access the global $counter
variable:

function updateCounter()
{
global $counter;
$counter++;
}
$counter = 10;
updateCounter();
echo $counter;
11
 A more cumbersome way to update the global variable is to use PHP’s $GLOBALS
array instead of accessing the variable directly:

function updateCounter()
{
$GLOBALS[counter]++;
}
$counter = 10;
updateCounter();
echo $counter;
11

Data Types
PHP provides eight types of values, or data types. Four are scalar (single-value) types:
integers, floating-point numbers, strings, and Booleans. Two are compound (collection)
types: arrays and objects. The remaining two are special types: resource and NULL.
1. Integers
Integers are whole numbers, such as 1, 12, and 256. The range of acceptable values
varies according to the details of your platform but typically extends from
−2,147,483,648 to +2,147,483,647. Specifically, the range is equivalent to the range of
the long data type of your C compiler. Unfortunately, the C standard doesn’t specify
what range that long type should have, so on some systems you might see a different
integer range.
Integer literals can be written in decimal, octal, or hexadecimal. Decimal values are
represented by a sequence of digits, without leading zeros. The sequence may begin
with a plus (+) or minus (−) sign. If there is no sign, positive is assumed. Examples of
decimal integers include the following:
1998
−641
+33

2. Floating-Point Numbers
Floating-point numbers (often referred to as real numbers) represent numeric values with
decimal digits. Like integers, their limits depend on your machine’s details. PHP floating-point
numbers are equivalent to the range of the double data type of your C compiler. Usually, this
allows numbers between 1.7E−308 and 1.7E+308 with 15 digits of accuracy. PHP recognizes
floating-point numbers written in two different formats. There’s the one we all use every day:
3.14
0.017
-7.1
but PHP also recognizes numbers in scientific notation:
0.314E1 // 0.314*10^1, or 3.14
17.0E-3 // 17.0*10^(-3), or 0.017
Floating-point values are only approximate representations of numbers. For example, on many
systems 3.5 is actually represented as 3.4999999999. This means you must take care to avoid
writing code that assumes floating-point numbers are represented completely accurately, such as
directly comparing two floating-point values using ==. The normal approach is to compare to
several decimal places:

if (intval($a * 1000) == intval($b * 1000)) {


// numbers equal to three decimal places
}
Use the is_float() function (or its is_real() alias) to test whether a value is a floatingpoint
number:
if (is_float($x)) {
// $x is a floating-point number
}
3. Strings
Because strings are so common in web applications, PHP includes core-level support for creating
and manipulating strings. A string is a sequence of characters of arbitrary length. String literals
are delimited by either single or double quotes:
'big dog'
"fat hog"

Variables are expanded (interpolated) within double quotes, while within single quotes they are
not:

$name = "Guido";
echo "Hi, $name\n";
echo 'Hi, $name';
Hi, Guido
Hi, $name

Double quotes also support a variety of string escapes, as listed below.


Escape sequences in double-quoted strings
Escape sequence Character represented
\" Double quotes
\n Newline
\r Carriage return
\t Tab
\\ Backslash
\$ Dollar sign
\{ Left brace
\} Right brace
\[ Left bracket
\] Right bracket

4. Booleans

A Boolean value represents a “truth value”—it says whether something is true or not.
Like most programming languages, PHP defines some values as true and others as false.
Truth and falseness determine the outcome of conditional code such as:
if ($alive) { ... }

In PHP, the following values all evaluate to false:


• The keyword false
• The integer 0
• The floating-point value 0.0
• The empty string ("") and the string "0"
• An array with zero elements
• An object with no values or functions
• The NULL value

A value that is not false is true, including all resource values (which are described later
in the section “Resources” on page 28).
PHP provides true and false keywords for clarity:
$x = 5; // $x has a true value
$x = true; // clearer way to write it
$y = ""; // $y has a false value
$y = false; // clearer way to write it

Use the is_bool() function to test whether a value is a Boolean:

if (is_bool($x)) {
// $x is a Boolean
}

5. Arrays
An array holds a group of values, which you can identify by position (a number, with
zero being the first position) or some identifying name (a string), called an associative
index:
$person[0] = "Edison";
$person[1] = "Wankel";
$person[2] = "Crapper";
$creator['Light bulb'] = "Edison";
$creator['Rotary Engine'] = "Wankel";
$creator['Toilet'] = "Crapper";

The array() construct creates an array. Here are two examples:

$person = array("Edison", "Wankel", "Crapper");


$creator = array('Light bulb' => "Edison",
'Rotary Engine' => "Wankel",
'Toilet' => "Crapper");

There are several ways to loop through arrays, but the most common is a foreach loop:

foreach ($person as $name) {


echo "Hello, {$name}\n";
}
foreach ($creator as $invention => $inventor) {
echo "{$inventor} created the {$invention}\n";
}

Hello, Edison
Hello, Wankel
Hello, Crapper
Edison created the Light bulb
Wankel created the Rotary Engine
Crapper created the Toilet

6. 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;
?>
7. 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);
?>
8. Resource

The special resource type is not an actual data type. It is the storing of a reference to functions
and resources external to PHP.A common example of using the resource data type is a database
call.

You might also like