Friday 20 December 2019

UNIT – I : Chapter 1 Introduction to PHP


UNIT – I : Chapter 1 Introduction to PHP
1.1 Evaluation of Php
1.2 Basic Syntax
1.3 Defining variable and constant
1.4 Php Data type
1.5 Operator and Expression
1.1 Evaluation of Php:
PHP started out as a small open source project that evolved as more and more people found out how useful it was. Rasmus Lerdorf released the first version of PHP way back in 1994.
·        PHP is a recursive acronym for "PHP: Hypertext Preprocessor".
·        PHP is a server side scripting language that is embedded in HTML. It is used to manage dynamic content, databases, session tracking, even build entire e-commerce sites.
·        It is integrated with a number of popular databases, including MySQL, Oracle, Sybase, Informix, and Microsoft SQL Server.
·        PHP is pleasingly zippy in its execution, especially when compiled as an Apache module on the Unix side. The MySQL server, once started, executes even very complex queries with huge result sets in record-setting time.
·        PHP supports a large number of major protocols such as POP3, IMAP, and LDAP
·        PHP Syntax is C-Like.
Common uses of PHP
·        PHP performs system functions, i.e. from files on a system it can create, open, read, write, and close them.
·        PHP can handle forms, i.e. gather data from files, save data to a file, through email you can send data, return data to the user.
·        You add, delete, modify elements within your database through PHP.
·        Access cookies variables and set cookies.
·        Using PHP, you can restrict users to access some pages of your website.
·        It can encrypt data.
Characteristics of PHP
Five important characteristics make PHP's popular.
·         Simplicity
·         Efficiency
·         Security
·         Flexibility
·         Familiarity
1.2 Basic Syntax
PHP pages are generally HTML pages with PHP commands embedded in them. The web server processes the PHP commands and sends their output (and any HTML from the file) to the browser.



Example 1-1 shows a complete PHP page.
<html>
<head>
<title>Look Out World</title>
</head>
<body>
<?php echo "Hello, world!"; ?>
</body>
</html>
1.3 Defining variable and constant
Creating Variables
Creating a variable in PHP is known as declaring it. Declaring a variable is as simple as using its name in your script:
$my_first_variable;
When PHP first sees a variable’s name in a script, it automatically creates the variable at that point. Many programming languages prevent you from using a variable without first explicitly declaring (creating) it. But PHP lets you use variables at any point just by naming them.
When you declare a variable in PHP, it’s good practice to assign a value to it at the same time. This is known as initializing a variable. By doing this, anyone reading your code knows exactly what value the variable holds at the time it ’s created. If you don’ t initializes a variable in PHP, it’ s given the default value of null.
Here ’s an example of declaring and initializing a variable:
$my_first_variable = 3;
This creates the variable $my_first_variable , and uses the = operator to assign it a value of 3.
Looking back at the addition example earlier, the following script creates two variables, initializes them with the values 5 and 6 , then outputs their sum ( 11 ):
<html>
<head>
<title>Look Out World</title>
</head>
<body>
<?php
$x = 5;
$y = 6;
echo $x + $y;
?>
</body>
</html>
Constants
The values of constants, as their name implies, can never be changed. Constants can be defined only once in a PHP program.
Constants differ from variables in that their names do not start with the dollar sign, but other than that they can be named in the same way variables are.
However, it’s good practice to use all - uppercase names for constants.
Constants may only contain scalar values such as Boolean, integer, float, and string (not values such as arrays and objects), can be used from anywhere in your PHP program without regard to variable scope, and are case - sensitive.
To define a constant, use the define() function, and include inside the parentheses the name you’ve
chosen for the constant, followed by the value for the constant, as shown here:
define( “MY_CONSTANT”, “19” ); // MY_CONSTANT always has the string value “ 19 ”
echo MY_CONSTANT; // Displays “ 19 ” (note this is a string, not an integer)
Constants are useful for any situation where you want to make sure a value does not change throughout the running of your script. Common uses for constants include configuration files and storing text to display to the user.
<html>
<head>
<title>Look Out World</title>
</head>
<body>
<?php
define("MYPI",3.1415);
$radius = 4;
$diameter = $radius * 2;
$circumference = MYPI * $diameter;
$area = MYPI * pow( $radius, 2 );
echo "This circle has... <br/>";
echo "A radius of ". $radius ."<br/>";
echo "A diameter of ".$diameter . "<br/> ";
echo "A circumference of ". $circumference . "<br/> ";
echo "An area of " . $area . " <br/> ";
?>
</body>
</html>

1.4 Php Data type
All data stored in PHP variables fall into one of eight basic categories, known as data types. A variable’s data type determines what operations can be carried out on the variable’s data, as well as the amount of memory needed to hold the data.
PHP supports four scalar data types. Scalar data means data that contains only a single value. Here’s a list of them, including examples:
Scalar
Data Type Description
Example
Integer
A whole number
15
Float
A floating - point number
8.23
String
A series of characters
“Hello, world!”
Boolean
Represents either true or false
TRUE

As well as the four scalar types, PHP supports two compound types.

Compound data is data that can contain more than one value. The following table describes PHP’s compound types:
Finally, PHP supports two special data types, so called because they don’t contain scalar or compound data as such, but have a specific meaning:

Testing the Type of a Variable
You can determine the type of a variable at any time by using PHP’s gettype() function. To use
gettype() , pass in the variable whose type you want to test. The function then returns the variable’s
type as a string.
To pass a variable to a function, place the variable between parentheses after the function name — for example, gettype( $x ) .
You can also test a variable for a specific data type using PHP ’ s type testing functions:

Changing Type by Casting
There is a settype() Function for Changing or Setting the type of a variable in PHP.
In the following example, a variable’s value is cast to various different types at the time that the
value is displayed:
$test_var = 8.23;
echo $test_var . “ < br / > ”; // Displays “8.23”
echo (string) $test_var . “ < br / > ”; // Displays “8.23”
echo (int) $test_var . “ < br / > ”; // Displays “8”
echo (float) $test_var . “ < br / > ”; // Displays “8.23”
echo (boolean) $test_var . “ < br / > ”; // Displays “1”
Note that $test_var ’s type isn’t changed at any point; it remains a floating - point variable, containing the value 8.23, at all times. All that changes is the type of the data that’s passed to the echo statement.
Here’s the full list of casts that you can use in PHP:

1.5 Operator and Expression
Using an operator, you can manipulate the contents of one or more variables to produce a new value. For example, this code uses the addition operator ( + ) to add the values of $x and $y together to produce a new value:
echo $x + $y;
So an operator is a symbol that manipulates one or more values, usually producing a new value in the process. Meanwhile, an expression in PHP is anything that evaluates to a value; this can be any combination of values, variables, operators, and functions.
For example, $x + $y is an expression. Here are some more examples of expressions:
$x + $y + $z
$x - $y
$x
5
true
gettype( $test_var )
The values and variables that are used with an operator are known as operands.
Operator Types
Operators in PHP can be grouped into ten types, as follows:

Arithmetic Operators
In PHP, the arithmetic operators (plus, minus, and so on) work much as you would expect, enabling you to write expressions as though they were simple equations.
For example, $c = $a + $b adds $a and $b and assigns the result to $c .
Here’s a full list of PHP ’ s arithmetic operators:

Assignment Operators
The basic assignment operator (=) can be used to assign a value to a variable:
$test_var = 8.23;
It’s also worth noting that the preceding expression evaluates to the value of the assignment: 8.23. This is because the assignment operator, like most operators in PHP, produces a value as well as carrying out the assignment operation.
This means that you can write code such as:
$another_var = $test_var = 8.23;
which means: “ Assign the value 8.23 to $test_var , then assign the result of that expression (8.23) to $another_var . ” So both $test_var and $another_var now contain the value 8.23 .
The equals sign (=) can be combined with other operators to give you a combined assignment operator that makes it easier to write certain expressions. The combined assignment operators (such as +=, – =, and so on) simply give you a shorthand method for performing typical arithmetic operations, so that you don’t have to write out the variable name multiple times.
For example, you can write:
$first_number += $second_number;
rather than:
$first_number = $first_number + $second_number;
This also works for other kinds of operators.
Bitwise Operators:
Here’s a list of the comparison operators in PHP:

The following examples show comparison operators in action:
$x = 23;
echo ( $x < 24 ) . “ < br / > ”; // Displays 1 (true)
echo ( $x < “24 ” ) . “ < br / > ”; // Displays 1 (true) because // PHP converts the string to an integer
echo ( $x == 23 ) . “ < br / > ”; // Displays 1 (true)
echo ( $x === 23 ) . “ < br / > ”; // Displays 1 (true)
echo ( $x === “23 ” ) . “ < br / > ”; // Displays “” (false) because // $x and “23” are not the same data type
echo ( $x > = 23 ) . “ < br / > ”; // Displays 1 (true)
echo ( $x > = 24 ) . “ < br / > ”; // Displays “” (false)

Incrementing /Decrementing Operators
Many times it’s useful to add or subtract the value 1 (one) over and over. This situation occurs so
Frequently. for example, when creating loops that special operators are used to perform this task: the increment and decrement operators.
They are written as two plus signs or two minus signs, respectively, preceding or following a variable name, like so:

++$x; // Adds one to $x and then returns the result
$x++; // Returns $x and then adds one to it
– - $x; // Subtracts one from $x and then returns the result
$x – - ; // Returns $x and then subtracts one from it

The location of the operators makes a difference. Placing the operator before the variable name causes the variable’s value to be incremented or decremented before the value is returned; placing the operator after the variable name returns the current value of the variable first, then adds or subtracts one from the variable. For example:
$x = 5;
echo ++$x; // Displays “6” (and $x now contains 6)
$x = 5;
echo $x++; // Displays “5” (and $x now contains 6)
Interestingly, you can use the increment operator with characters as well. For example, you can “add” one to the character B and the returned value is C. However, you cannot subtract from (decrement)character values.

Logical Operators
The PHP logical operators are used to combine conditional statements.

Here are some simple examples of logical operators in action:
$x = 2;
$y = 3;
echo ( ($x > 1) & & ($x < 5) ) . “ < br / > ”; // Displays 1 (true)
echo ( ($x == 2) or ($y == 0) ) . “ < br / > ”; // Displays 1 (true)
echo ( ($x == 2) xor ($y == 3) ) . “ < br / > ”; // Displays “” (false) because both
// expressions are true
echo ( !($x == 5 ) ) . “ < br / > ”; // Displays 1 (true) because
// $x does not equal 5

String Operators
PHP has two operators that are specially designed for strings.

Understanding Operator Precedence
With simple expressions, such as 3 + 4 , it ’ s clear what needs to be done (in this case, “ add 3 and 4 to produce 7 ” ).
Consider the following example:
3 + 4 * 5
Is PHP supposed to add 3 to 4 to produce 7, then multiply the result by 5 to produce a final figure of 35?
Or should it multiply 4 by 5 first to get 20, then add 3 to make 23?
All PHP operators are ordered according to precedence.
An operator with a higher precedence is executed before an operator with lower precedence.

In the case of the example, * has a higher precedence than + , so PHP multiplies 4 by 5 first, then adds 3 to the result to get 23.
Here’s a list of all the operators you’ve encountered so far, in order of precedence (highest first):

You can affect the order of execution of operators in an expression by using parentheses. Placing
parentheses around an operator and its operands forces that operator to take highest precedence. So, for example, the following expression evaluates to 35:
(3 + 4) * 5.

No comments:

Post a Comment