In this example, each conditional block only consists of one line of code. Try changing the $animal variable between "Cat" and "Dog" to see the difference. In this case, we look for a certain animal, the dog, and prints a status message depending on whether or not we found it. As you saw in the first example, the else part is not required if you don't need it. Whenever the if statement doesn't evaluate to true, the else statement(s) are executed, if there are any. If one of the ways is not good enough, why not go the other way? The else statement is the if statements best friend - it's the alternative to a failed if statement. I'm sure that seems a bit complicated right now, but just try to remember it for now - in the beginning, the double equal sign is pretty much all you need. But now that we use three equal signs, the types are no longer the same, since one is an integer and the other is a text string. ![]() Here is an example to show you the difference: Īll we did was compare the variable against a string containing "42" instead of the number 42. If you use three of them, PHP will compare both value and type and only if both of them match, the expression is true. The difference is if you use two equal signs, PHP will only look to see if the values are identical. PHP actually takes it one step further, introducing the triple equal sign. Had we only used one equal sign, 42 would be assigned to $number, and no comparison would have been made. So, in this case, we use two equal signs to compare the two items: $number and 42. Two equal signs are a comparison operator, that is, it's used to compare two values. If you change the number in the first line to something else, you will see that the echo line is not executed.Īs you can see, we are using two equal signs after each other - why is that? In PHP, and in many other programming languages, a single equal sign is an assignment operator. The next line of code will only be executed if the condition evaluates to true. In the next line, we use the if statement to check if the number is what we expect. We declare a variable called number and assign the value 42 to it. If not, here are some examples to get you started. If you have already used another programming language, chances are that you can use the PHP if statement straight away, since it looks pretty much the same as in other programming languages. Being able to set up conditional blocks of code is a fundamental principal of writing software. To make a similar function, array_concatenate(), change only the first of the two '+=' in array_add() to '.One of the single most important statements in every programming language is the "if" statement. $aDiff = array_subtract ( $a1, $a2, $a3 ) ![]() Getting Started Introduction A simple tutorial Language Reference Basic syntax Types Variables Constants Expressions Operators Control Structures Functions Classes and Objects Namespaces Enumerations Errors Exceptions Fibers Generators Attributes References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Predefined Attributes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting User Submitted Data Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts ? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto searchĪ more compact way of adding or subtracting the elements at identical keys.įunction array_subtract ( $a1, $a2 )
0 Comments
Leave a Reply. |