Читать книгу PHP Programming for Beginners. Key Programming Concepts. How to use PHP with MySQL and Oracle databases (MySqli, PDO) - Sergey D Skudaev - Страница 5

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.


$a = “10”;

$b=10;

if ($a ==$b)

echo “a = b <br>”;

else

echo “a <> b <br>”;

If ($a === $b)

echo “a and b are identical <br>”;

else

echo “a and b are not identical <br>”;


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.


<?php

// $F is a temperature in Fahrenheit, $C is in Celsius

$F=80;

//The “echo” is a command that Output: s text to

//the browser

echo “F=”. $F.”<br>”;

// a formula to convert Fahrenheit to Celsius

$C = ($F – 32) * 5/9;

//Output: temperature in Celsius

echo “C=”. $C.”<br>”;

?>


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">yahoo</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 the 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

require("footer.php”);

or

require_once ("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. The require_once is similar to the required?? but it checks to include the footer.php file only once even if you write require_once ("footer.php”); twice.

PHP Programming for Beginners. Key Programming Concepts. How to use PHP with MySQL and Oracle databases (MySqli, PDO)

Подняться наверх