Você está na página 1de 25

WT UNI T VI III I.

T Introduction to PHP
PHP, or PHP: Hypertext Preprocessor, is a cross-platform server side scripting language that is primarily used to create dynamic and interactive web pages, which means that it can be used on almost every major operating system. It can be embedded into HTML and is generally run on web servers that are running the Apache HTTP server software. It is free software and is free to download and use. PHP is a recursive acronym for "PHP: Hypertext Preprocessor". PHP is an open source, server side scripting language that is embedded in HTML. It is used to manage dynamic content, databases, session tracking, even build entire ecommerce sites. PHP is compatible with all the major web servers like Apache, Microsoft IIS. It is integrated with a number of popular databases, including MySQL, PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server. PHP supports a large number of major protocols such as POP3, IMAP, and LDAP. PHP4 added support for Java and distributed object architectures (COM and CORBA), making n-tier development a possibility for the first time. PHP Syntax is very similar to C and Perl languages.

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 forms, save data into database. We can add, delete, and modify elements within our database through PHP. Through PHP, we can access cookies variables and can set cookies. Using PHP, we can restrict users to access some pages of our website. It can encrypt data.

Characteristics of PHP
Five important characteristics make PHP's practical nature possible: Simplicity Efficiency Security Flexibility Familiarity Familiarity Programmers from many backgrounds will already be familiarized with the PHP language. Many of the languages constructs are borrowed from C and Perl, and in many cases PHP code is almost indistinguishable from the typical C program. Simplicity There is no need to include libraries, special compilation directives, or anything. The PHP engine simply begins executing the code after the first escape sequence (<?php). If the code is syntactically correct, it will be executed exactly as it is displayed.

1 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T


Efficiency Efficiency is an extremely important consideration for working in a multiuser environment such as the WWW. PHP 4.0 introduced resource allocation mechanisms and more support for object-oriented programming, in addition to session management features. Reference counting has also been introduced in the latest version, eliminating unnecessary memory allocation. Security PHP provides developers and administrators with a flexible and efficient set of security safeguards. These safeguards can be divided into two frames of reference: system level and application level. Flexibility Because PHP is an embedded language, it is extremely flexible towards meeting the needs of the developer. Although PHP is generally used in conjunction with HTML, it can also be integrated with the languages like JavaScript, WML, XML, and many others. Browser dependency is not an issue because PHP scripts are compiled entirely on the server side before being sent to the user.

Server side Processing with PHP


Generally, PHP works in partnership with a web server, which enables us to build interactive and dynamic web pages. A web server is the software that delivers web pages to our website's users. The PHP software works in conjunction with the web server to allow the functionality necessary to deliver the interactive and dynamic web pages. When a website user goes to a web page with a .php extension, for example by typing the URL into the web browser directly or by clicking a link, the request is sent to a web server. That request is then sent to the PHP interpreter. The PHP interpreter also communicates with file systems, databases and email servers as needed, and then delivers the request to the web server to return to the web browser. In comparison, when a website user goes to a purely HTML generated web page; the server sends HTML data to the web browser without having the code interpreted.

1. The user enters a web page address in the browsers location bar. 2. The browser breaks apart that address and sends the name of the page to the host. For example, http://www.phone.com/login.php requests the page login.php from www.phone.com. 3. The web server process on the host receives the request for login.php. 4. The web server reads the login.php file from the hosts hard drive. 5. The web server detects that the PHP file isnt just a plain HTML file, so it asks another processthe PHP interpreterto process the file.

2 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T


6. The PHP interpreter executes the PHP code that it finds in the text it received from the web server process. Included in that code are calls to the MySQL database. 7. PHP asks the MySQL database process to execute the database calls. 8. The MySQL database process returns the results of the database query. 9. The PHP interpreter completes execution of the PHP code with the data from the database and returns the results to the web server process. 10. The web server returns the results in the form of HTML text to the browser. 11. The web browser uses the returned HTML text to build the web page on users screen.

PHP Environment
In order to develop and run PHP Web pages, three vital components need to be installed on our computer system. Web Server - PHP will work with virtually all Web Server software, including Microsoft's Internet Information Server (IIS) but then most often used is freely available Apache Server. Database - PHP will work with virtually all database software, including Oracle and Sybase but most commonly used is freely available MySQL database. PHP Parser - In order to process PHP script instructions a parser must be installed to generate HTML output that can be sent to the Web Browser.

XAMPP Installation
XAMPP is an easy-to-use multi-platform package that installs Apache, MySQL, PHP, phpMyAdmin, and other software useful for dynamic web development on the computer. Installation is painless, and configuration minimal. There are some steps to be followed for installing XAMPP in Windows 1. Download the Basic Package XAMPP MSI installer found http://www.apachefriends.org/en/xampp-windows.html. 2. Double-click the MSI installer file on the desktop, and well see the installer.

at

3. Select English and click the OK button. 4. The Setup Wizard appears as below figure. Click Next.

3 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T

5. The below dialog box is displayed. Click Next to accept the default installation directory.

6. The XAMPP Options dialog displays. Leave the Service section checkboxes unchecked so we dont install the components as services; instead, well start them from the Control Panel. Click Install.

4 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T

7. The Completing the XAMPP Setup Wizard displays. Click Finish. 8. The option to start the Control Panel displays as the below figure. Click Yes.

9. The Control Panel launches, as shown below. The Control Panel can be used to start and stop the services, as well as aid in their configuration

5 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T

PHP Syntax
The syntax for PHP scripting blocks start with a <?php delimiter and end with a ?> delimiter. A PHP scripting block can be placed anywhere within an HTML document. If PHP is included within an HTML document, the document must have a .php extension. <?php // PHP scripting block ?> There are other PHP delimiters that can be used, however, it is recommended that you use the standard form <?php and ?> delimiters for maximum compatibility. Other PHP delimiters that we can use are Shorthand delimiters (<? ?>). ASP style delimiters (<% %>) Placing our code inside <script> tags with the language attribute set to php. The examples below show us how each style of PHP delimiter looks. On servers that have shorthand support enabled, we can use the scripting block delimiters <? and ?>. Syntax: <? // PHP code ?> We can use the ASP style delimiters <% and %> by turning on the asp_tags directive. Syntax: <% // PHP code %> We can also place PHP code within the <script> and </script> tags with the language attribute set to php Syntax <script language="php"> // PHP code </script> Each PHP statement must end with a semi-colon (;). This tells the PHP interpreter that that PHP statement has ended. If a semi-colon is not included at the end of the line of code, the interpreter will assume that the PHP statement continues onto the next line. The PHP interpreter condenses all sequential whitespace in PHP scripts to a single whitespace. This allows programmers to structure their code into a more readable format.

6 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T PHP comment tags


PHP allows us to add comments that are visible to us, the programmer, to PHP code without the PHP interpreter trying to execute the comments as code. The PHP comments tags are // for single-line comments and enclosing /* and */ tags for multiple-line comments. <?php // this is a single-line comment ?> <?php /* this is a multiple-line comment this is a multiple-line comment */ ?>

Running PHP Scripts


To run the PHP scripts in XAMPP, we need to follow the steps like Step 1: Start XAMPP Control Panel Application and start Apache and MySQL services. Step 2: 1. To run PHP code or PHP script we need a .php file. So create a file with a name called "myFirstPHPScript.php". For example <?php echo "Hello World! This is my first PHP code snippet!!"; ?> 2. Save this file in htdocs folder available in the XAMPP installation directory i.e c:\xampp. Every PHP file or PHP Project must be stored in this htdocs directory. If it a big project then keeps all the PHP, HTML, CSS, JavaScript files in one folder and place them in the htdocs directory.

Step 3: Open any web browser(Chrome, IE, Firefox, Safari, Opera etc.) . Simply type http://localhost/myFirstPHPScript.php in the address bar. Then we get the page like

7 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T

PHP Variables
Variables are used for storing values, such as numbers, strings or function results, so that they can be used many times in a script. Once a variable is declared, it can be used over and over again

throughout your script. Declaration of Variables in PHP To declare a variable in PHP, first type the dollar sign symbol ($), followed by the name of the variable. The name of the variable is followed by the assignment operator (=), which is followed by the value that you want to assign to the variable, followed by the semicolon (;). Variable Naming Conventional and Rules A variable name must start with a letter or an underscore "_" A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, _) A variable name should not contain spaces. If a variable name should be more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString) The variable names are case-sensitive, so $myvar is different from $myVar.

The syntax of PHP variables is <?php $variable_name = value; ?> Variable Types PHP has a total of eight data types which we use to construct our variables. Out of eight data types, four are scalar types, two are compound types and other two are special types. Scalar Data types: Integers: are whole numbers, without a decimal point, like 4195. Doubles: are floating-point numbers, like 3.14159 or 49.1. Booleans: have only two possible values either true or false. Strings: are sequences of characters, like 'PHP supports string operations.

8 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T


Compound Types Arrays : are the variables to hold the multiple values Objects: are instances of programmer-defined classes, which can packed up both the values and functions that are specific to the class. Special Types NULL: is a special type that only has one value: NULL. Resources: are special variables that hold references to resources external to PHP such as database connections. Integers They are whole numbers, without a decimal point, like 4195 represented in both positive and negative. Integers can be assigned to variables, or they can be used in expressions, like so: $int_var = 12345; $another_int = -12345 + 12345; Integer can be in decimal (base 10), octal (base 8), and hexadecimal (base 16) format. Decimal format is the default, octal integers are specified with a leading 0, and hexadecimals have a leading 0X. For most common platforms, the largest integer is 2,147,483,647, and the smallest integer is -2,147,483,647. Doubles They like 3.14159 or 49.1. By default, doubles print with the minimum number of decimal places needed. For example, the code: $many = 2.2888800; $many_2 = 2.2111200; $few = $many + $many_2; print(.$many + $many_2 = $few<br>.); It produces the following browser output: 2.28888 + 2.21112 = 4.5 Boolean They have only two possible values either TRUE or FALSE. if (TRUE) print("This will always print<br>"); else print("This will never print<br>");

9 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T


Interpreting other types as Booleans Here are the rules for determine the "truth" of any value not already of the Boolean type: If the value is a number, it is false if exactly equal to zero and true otherwise. If the value is a string, it is false if the string is empty (has zero characters) or is the string "0", and is true otherwise. Values of type NULL are always false. If the value is an array, it is false if it contains no other values, and it is true otherwise. For an object, containing a value means having a member variable that has been assigned a value. Don't use double as Booleans. Each of the following variables has the Boolean value embedded in its name when it is used in a Boolean context. $true_num = 3 + 0.14159; $true_str = "Tried and true" $true_array[49] = "An array element"; $false_array = array(); $false_null = NULL; $false_num = 999 - 999; $false_str = ""; Strings They are sequences of characters, like "PHP supports string operations". Following are valid examples of string. $string_1 = "This is a string in double quotes"; $string_2 = "This is a somewhat longer, singly quoted string"; $string_39 = "This string has thirty-nine characters"; $string_0 = ""; // a string with zero characters Singly quoted strings are treated almost literally, whereas doubly quoted strings replace variables with their values as well as specially interpreting certain character sequences. <? $variable = "name"; $literally = 'My $variable will not print!\\n'; print($literally); $literally = "My $variable will print!\\n"; print($literally); ?> This will produce following result: My $variable will not print!\n My name will print

10 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T


Strings that are delimited by double quotes (as in "this") are preprocessed in the following two ways by PHP: Certain character sequences beginning with backslash (\) are replaced with special characters Variable names (starting with $) are replaced with string representations of their values. The escape-sequence replacements are: \n is replaced by the newline character \r is replaced by the carriage-return character \t is replaced by the tab character \$ is replaced by the dollar sign itself ($) \" is replaced by a single double-quote (") \\ is replaced by a single backslash (\) NULL NULL is a special type that only has one value: NULL. To give a variable the NULL value, simply assign it like this: $my_var = NULL; The special constant NULL is capitalized by convention, but actually it is case insensitive; you could just as well have typed: $my_var = null; A variable that has been assigned NULL has the following properties: It evaluates to FALSE in a Boolean context. It returns FALSE when tested with IsSet() function. Arrays In PHP an array can have numer ic key, associative key or both. The value of an array can be of any type. To create an array use the array() language construct like this. <?php $numbers = array(1, 2, 3, 4, 5, 6); $age = array("mom" => 45, "pop" => 50, "bro" => 25); $mixed = array("hello" => "World", 2 => "It's two"; echo "numbers[4] = {$numbers[4]} <br>"; echo "My mom's age is {$age['mom']} <br>"; echo "mixed['hello'] = {$mixed['hello']} <br>"; echo "mixed[2] = {$mixed[2'}"; ?>

11 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T


Scope of the Variables Every variable that is defined in a PHP script within an area of accessibility called a scope. The scope of that variable determines which elements in that script can access that variable. PHP variables can be one of four scope types: Local variables Global variables Super Global Variables Local Variables Variables that are defined in the body of a function are local for the function. Local variables are not accessible in the global scope. Global Variables Variables that are defined in the main body of a script are global variables. Global variables are not accessible in the local scope. To show the difference between local and global variables, the below PHP code demonstrated <?php $globalVar = 1; function localFunc() { echo "The value in localFunc is: $globalVar.<br />"; } function globalFunc() { global $globalVar; echo "The value in globalFunc is: $globalVar"; } localFunc(); globalFunc(); ?> The PHP code above outputs: The value in localFunc is:The value in globalFunc is: 1

12 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T


Super Global Variables Superglobal variables are built-in arrays that have a global scope. Superglobal arrays are available anywhere in the program, including in functions. Array Description $_POST Contains all of the variables that are submitted in a form using the POST method. $_GET Contains all of the variables that are submitted in a form using the GET method. $_COOKIE Contains all of the cookie variables. $_REQUEST Contains all of the variables that are in the $_POST, $_GET and $_COOKIE. $_FILES Contains the names of files that have been uploaded. $_SERVER Contains information about your server. $_ENV Contains information that is provided by your operating system. $GLOBALS Contains all of the global variables. Useful Functions for Variables PHP has a number of built-in functions for working with variables. gettype() gettype() determines the data type of a variable. It returns one of the following values: "integer" "double" "string" "array" "object" "class" "unknown type" An example using gettype() may be: if (gettype ($user_input) == "integer") { $age = $user_input; } settype() The settype() function explicitly sets the type of a variable. The type is written as a string and may be one of the following: array, double, integer, object or string. If the type could not be set then a false value is returned. $a = 7.5; // $a is a double settype($a, "integer"); // Now it's an integer (value 7) settype() returns a true value if the conversion is successful. Otherwise it returns false. if (settype($a, "array")) { echo("Conversion succeeded."); } else { echo ("Conversion error."); }

13 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T


isset() and unset() unset() is used to destroy a variable, freeing all the memory that is associated with the variable so it is then available again. The isset() function is used to determine whether a variable has been given a value. If a value has been set then it returns true. $ProductID = "432BB"; if (isset($ProductID)) { echo("This will print"); } unset($ProductID); if (isset ($ProductID)) { echo("This will NOT print"); } The is...() functions The functions is_int(), is_integer(), and is_long() are all synonymous functions that determine whether a variable is an integer. is_double(), is_float(), and is_real() determine whether a variable is a double. is_string(), is_array(), and is_object() work similarly for their respective data types. $ProductID = "432BB"; if (is_string ($ProductID)) { echo ("String"); }

PHP Constants
A PHP constant is a variable that cannot be changed after the initial value is assigned during the execution of the script. We can create a constant variable using the define () function. The function accepts 2 arguments. Syntax: define (<constant name>, <value>); The first must be a string and represents the name which will be used to access the constant. A constant name is case-sensitive by default and by convention, is always uppercase. A valid constant name starts with a letter or underscore, followed by any combination of letters, numbers or underscores. Constant names must be in quotes when they are defined. The second argument is the value of the constant and can be a string or numbers and can be set explicitly. The scope of a constant is global, which means that we can access constants anywhere in our script without regard to scope. <?php define ("WEBSITE", "WebDevelopmentTutorials.com"); define(PI,3.17); echo WEBSITE; EchoPI value is:.PI;?>

14 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T PHP Operators


In PHP, operators are used to manipulate or perform operations on values and expressions. Operators allow us to assign values to variables, perform arithmetic operations, concatenate strings, compare values and perform Boolean operations. PHP Assignment Operators PHP Arithmetic Operators PHP String Concatenation Operator PHP Increment and Decrement Operators PHP Logical Operators PHP Comparison Operators PHP Assignment Operators The assignment operator (= or equals), is used to assign a value to a variable or to assign a variable to another variable's value. The assignment operator can also be used in combination with other mathematical operators to change the value of a variable. = (assigment) operator The = (assignment) operator sets the value of the first operand to the value of the second operand. <?php $x = 1; echo $x;?> The code above outputs: 1 += (addition-assignment) operator The += (addition-assigment) operator sets the value of the first operand to its value plus the second value. <?php $x = 1; $x += 2; echo $x; ?> The code outputs: 3 -= (subtraction-assignment) operator The -= (subtraction-assignment) operator sets the value of the first operand to its value minus the secode value.

15 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T


<?php $x = 3; $x -= 2; echo $x; ?> The code above outputs: 1 *= (multiplication-assignment) operator The *= (multiplication-assignment) operator sets the value of the first operand to its value multiplied by the second value. <?php $x = 2; $x *= 2; echo $x; ?> The code above outputs: 4 /= (division-assignment) operator The /= (division-assignment) operator sets the value of the first operancd to its value divided by the second value. <?php $x = 4; $x /= 2; echo $x; ?> The code above outputs: 2 .= (concatenation-assignment) operator The .= (concatenation-assignment) operator sets the value of the first operand to a string containing its value with the second value appended at the end. <?php $x = "WebDevelopmentTutorials.com "; $x .= "is the best."; echo $x; ?>

16 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T


The code above outputs: WebDevelopmentTutorials.com is the best. %= (modulo-assignment) operator The %= (modulo-assignment) operator sets the value of the first operand to the remainder of its value divided by the second value. <?php $x = 3; $x %= 2; echo $x; ?> The code above outputs: 1

Operator =

Description Simple assignment operator, Assigns values from right side operands to left side operand Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operandAND assignment operator, It Divide divides left operand with the right operand and assign the result to left operand Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand

Example C = A + B will assign value of A + B into C C += A is equivalent to C = C + A

+=

-=

C -= A is equivalent to C = C - A

*=

C *= A is equivalent to C = C * A

/=

C /= A is equivalent to C = C / A

%=

C %= A is equivalent to C = C % A

PHP Arithmetic Operators PHP arithmetic operators are used to perform mathematical operations. +(addition) operator The +(addition) operator calculates the sum of 2 values. <?php echo 1 + 1; ?>

17 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T


The code above outputs: 2 -(subtraction) operator The -(subtraction) operator calculates the difference between 2 values. <?php echo 2 - 1; ?> The code above outputs: 1 *(multiplication) operator The PHP *(multiplication) operator multiplies 2 values. <?php echo 2 * 2; ?> The code above outputs: 4 /(division) operator The PHP /(division) operator divides the first value by the second value. <?php echo 4 / 2; ?> The code above outputs: 2 %(modulus) operator The PHP %(modulus) operator calculates the remainder of the division of the first value by the second. <?php echo 3 % 2; ?> The code above outputs:

18 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T


1
Operator + * / % ++ -Description Adds two operands Subtracts second operand from the first Multiply both operands Divide numerator by denominator Modulus Operator and remainder of after an integer division Increment operator, increases integer value by one Decrement operator, decreases integer value by one Example A + B will give 30 A - B will give -10 A * B will give 200 B / A will give 2 B % A will give 0 A++ will give 11 A-- will give 9

PHP String Concatenation Operator The PHP concatenation operator (.) is used to combine 2 sting values to create one string. The concatenation operator us useful when WE are combining strings with values from constants and arrays. <?php $variable1="Hello"; $variable2="World"; echo $variable1 . " " . $variable2; ?> The code above outputs: Hello World

PHP Increment and Decrement Operators PHP increment and decrement operators are used to increment and decrement numeric values. ++$value(pre-increment) operator The PHP ++$value(pre-increment) operator adds 1 to a value before the value is used in the expression in which it is contained. <?php $value = 1; echo ++$value; ?> The code above outputs: 2

19 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T


$value++(post-increment) operator The PHP $value++(post-increment) operator adds 1 to a value after the value is used in the expression in which it is contained. <?php $value = 1; echo $value++; echo "<br />"; echo $value; ?> The code above outputs: 1 2 --$value(pre-decrement) operator The PHP --$value(pre-decrement) operator subtracts 1 from a value before the value is used in the expression in which it is contained. <?php $value = 2; echo --$value; ?> The code above outputs: 1 $value--(post-drecrement) operator The PHP $value--(post-drecrement) operator subtracts 1 from a value after the value is used in the expression in which it is contained. <?php $value = 2; echo $value--; echo "<br />; echo $value; ?> The code above outputs: 2 1

20 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T


PHP Logical Operators PHP logical operators are used to perform Boolean operations to compare one value to another to determine if a statement is true or false. AND operator The PHP and operator checks if all expression values are true. <?php $a = 1; $b = 2; if (($a == 1) and ($b == 2)) echo "True"; ?> The code above outputs: True && operator The PHP && operator checks if all expression values are true. This operator does the same thing as the and operator. <?php $a = 1; $b = 2; if (($a == 1) && ($b == 2)) echo "True"; ?> OR operator The PHP or operator checks if any expression values are true. <?php $a = 1; $b = 2; if (($a == 1) or ($b == 100)) echo "True"; ?> The code above outputs: True || operator The PHP || operator checks if any expression values are true. This operator does the same thing as the OR operator. <?php $a = 1;

21 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T


$b = 2; if (($a == 1) || ($b == 100)) echo "True"; ?> The code above outputs: True XOR The PHP XOR operator checks if only one expression value is true. <?php $a = 1; $b = 2; if (($a == 1) xor ($b == 100)) echo "True"; ?> The code above outputs: True ! operator The PHP ! operator checks if a statement is false <?php $a = 1; if (!($a == 1)) echo "True"; ?> The code above outputs: True

Operator and

Description Called Logical AND operator. If both the operands are true then then condition becomes true. Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. Called Logical AND operator. If both the operands are non zero then then condition becomes true. Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.

Example (A and B) is true.

or

(A or B) is true.

&&

(A && B) is true.

||

(A || B) is true.

!(A && false.

B)

is

22 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T


PHP Comparison Operators PHP comparison operators compare values and return Boolean true or false values depending on the result. == (equal to) operator The == (equl to) operator checks if the first operand is equal to the second operand. <?php $a = 1; $b = 1; if ($a == $b) echo "$a is equal to $b."; ?> The code above outputs: <?php 1 is equal to 1. ?> === (identical to) operator The PHP === (identical to) operator checks if the first operand has the same value and type as the second operand. <?php $a = 1; $b = 1; if ($a === $b) echo "1 is identical to 1."; ?> The code above outputs: 1 is identical to 1. != (not equal to) operator The PHP != (equal to) operator checks if the first operand is not equal to the second operand. <?php $a = 1; $b = 2; if ($a != $b) echo "$a is not equal to $b."; ?> The code above outputs: 1 is not equal to 2.

23 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T


!== (not identical to) operator The PHP !== (not identical to) operator checks if the first operand is not equal to the second operand. <?php $a = 1; $b = 2; if ($a !== $b) echo "$a is not identical to $b."; ?> The code above outputs: 1 is not equal to 2. < (less than) operator The PHP < (less than) operator checks if the first operand is less than the second operand. <?php $a = 1; $b = 2; if ($a < $b) echo "$a is less than $b."; ?> The code above outputs: 1 is less than 2. > (greater than) operator The PHP > (greater than) operator checks if the first operand is greater than the second operator. <?php $a = 2; $b = 1; if ($a > $b) echo "$a is greater than $b."; ?> The code above outputs: 2 is greater than 1. <= (less than or equal to) operator The PHP <= (less than or equal to) operator checks if the first operand is less than or equal to the second operand. <?php $a = 1; $b = 20;

24 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

WT UNI T VI III I.T


if ($a <= $b) echo "$a is less than or equal to $b."; ?> The code above outputs: 1 is equal to or less than 20. >= (greater or equal to) operator The PHP >= (greater or equal to) operator checks if the first operand is greater than or equal to the second operand. <?php $a = 5; $b = 1; if ($a >= $b) echo "$a is greater than or equal to $b."; ?> The code above outputs: 5 is greater than or equal to 1.
Operator == Description Checks if the values of two operands are equal or not, if yes then condition becomes true. Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. Example (A == B) is not true. (A != B) is true.

!=

>

(A > B) is not true.

<

(A < B) is true.

>=

(A >= B) is not true. (A <= B) is true.

<=

25 S.RAVI KUMAR, ASST.PROF, DEPT.OF.I.T

Você também pode gostar