Friday, January 26, 2018

PHP comparison operators

Basic operators

Any computer language uses operators to calculate or compare variables, most of which are self explanatory.

Example:-

PHP uses + (plus),
 - (minus), 
* (multiplication), 
/ (division), 
%( modulo) and "=" the assignment operator.


Let's discuss the modulo operator. The modulo operator is used in finding the resultant remainder after one number has been divided into another. The following are a couple of examples to help you better understand:

The first example is 15 % 5 = 0. As you can see, 5 divides evenly into 15 with no remainder, so the result we get is 0. Now let's use 10 % 7 = 3 as our second example. Because 7 divides into 10 once and leaves a remainder of 3, our answer must be 3.

30 % 6 = 0 (30 - 5 * 6 = 30 30 - 30 = 0)
30 % 7 = 2 (30 - 4 * 7 = 30 - 28 = 2)
30 % 8 = 6 (30 - 3 * 8 = 30 - 24 = 6)

Comparison operators

◆.   $ a = = $ b Equal TRUE if $ a is equal to $ b after type adjustment.

◆.    $ a = = = $ b Identical TRUE if $ a is equal to $ b, and they are of the same type.

◆.   $ a != $ b Not equal TRUE if $ a is not equal to $ b after type adjustment.

◆.  $ a < > $ b Not equal TRUE if $ a is not equal to $ b after type adjustment.

◆.  $ a != = $ b Not identical TRUE if $ a is not equal to $ b, or they are not of the same type.

◆.   $ a < $ b Less than TRUE if $ a is strictly less than $ b.

◆.  $ a > $ b Greater than TRUE if $ a is strictly greater than $ b.

◆.    $ a < = $ b Less than or equal to TRUE if $ a is less than or equal to $ b.

◆.    $ a > = $ b Greater than or equal to TRUE if $ a is greater than or equal to $ b.

Let's see an example with = = = identical operator :-








OUTPUT:

a = b

a and b are not identical

$ a and $ b are equal because they equal 10, but they are not identical because $ a is a string and $ b is an integer.

$ a = 9; Assignment: $ a equals 9.

$ a + = 7; Addition-assignment: $ a equals $ a plus 7.

$ a *= 4; Multiplication-assignment: $ a equals $ a multiplied by 4.

$ a /= 3; Division-assignment: $ a equals $ a divided by 3.

$ a .= 6; Concatenation-assignment: $ a equals $ a concatenated with 6

Let's write a simple code converting Fahrenheit to Celsius:-






Output: -

F = 80

C = 26.6666666667

Please pay attention to "F =". $ F." < br >" on the code line echo; Here we concatenate 3 separate strings into one.

"F ="

$ F

"< br >" –HTML break.

Two dots are placed between our three strings because dots are used to concatenate strings in

PHP: "F =" $ F "< br >";

The fact is that $ F is not a string. It is an integer because it is equal to 80. If $ F were equal to "80" it would be a string.

When we concatenate the $ F variable between 2 strings, because PHP converts it to a string, there is no need to do anything further.

As I wrote earlier, a string of text can be enclosed in single quotation marks:

echo 'F ='. $ F.' < br >';

To output a string of text, you can use the "print" command instead of the "echo" command.

However, in this case, you would use parentheses and single or double quotation marks.

Print(' F ='. $ F.' < br />');

Or

print(" F =". $ F." < br />");

PHP variables are case sensitive, so $ F is not the same as $ f, and $ lastname is not the same as $ Lastname or $ LASTNAME.

Let us say that we wish to use PHP to output a web page to a browser using the following HTML link:

< a href =" http:// www.yahoo.com" > yahoo </ a >

If we write Print(" < a href =" http:// www.yahoo.com" > yanhoo </ a >");

We would receive an error message due to the use of double quotation marks in our HTML code. To avoid the error message, use single quotation marks for the print command as I've done in the following:-

print(' < a href =" http:// www.yahoo.com" > yanhoo </ a >');

The same is true for an HTML input element:

echo '< input type =" text" name =" lname" value =" James"/ >';

Or

print(' < input type =" text" name =" fname" value =" John"/ >');

One handy feature of PHP code is the ability to include one file inside another. The creation of a website solely in HTML requires you to edit every single page of that website if you later wish to change the navigation, logo or footer. The use of PHP to create the web pages allows you to include one file with navigation or logo in every web page requiring you to edit only one file to later change the navigation or logo.

There are two commands to include the file in your PHP code:

Include(' top_menu.php');

Or

Required(" footer.php");

The difference between the two is, when using the "include" command, if the file to be included does not exist, you will be given a warning message; however, the execution of the rest of the PHP code will not be interrupted. Conversely, when using the "required" command, if the required file is not found, a fatal error message is displayed and the execution of the PHP code will cease.Read More 

0 comments:

Please comment and follow this site blog inbox