Conditional Statements in PHP - PHP Virgin

Pubblicato il: 15 aprile 2014
sul canale di: CodeBabes
70,243
194

http://codebabes.com/courses/php-virg...

Before we get started with conditions, let's take a quick look at comparison operators. You should remember them from math class. These allow you to make a comparison between two values. For instance, 1 is less than 2, $a equals $b, or $a is not equal to 'dude bro'.

http://codebabes.com/courses/php-virg...

Programming is all about conditions. Hell, life is all about conditions. We think in conditions, right? Like when you think, IF I wear aviators, THEN I will look badass. Noone said you're logic is always correct, but hey, you get the idea.

So how does this work in PHP? Well, we have if else statements to do this. Let's go through one.

Here we have the if statement, the bread and butter of programming.

http://codebabes.com/courses/php-virg...

Start with the if keyword, then parenthesis, inside those we'll put our condition, cash money is greater than 100. Follow that with some braces, then print hit up the strip club. Next, we'll put elseif cash money is greater than 10, buy some beer to soak up your sadness. The else statement comes last, if all of our conditions fail. Maybe you should get a job with your new programming skills.

But what if you have A LOT of options, like I do, you know, cause I'm a total babe, and sort of a genius. Well, you could have a huge if and else if statement OR you could use the switch statement. It's a little more complicated, but easier to read.

Let's do one.

http://codebabes.com/courses/php-virg...

So how does this work? The switch statement takes a variable and then compares that too a bunch of different values. If the variable matches the case it will execute the code block of that case.

In the example we switch our cases based on the operating system given. If the statement is given OSX position it will print the text 'fanboy'.

If nothing matches you'll get the default. How did you get here, you're phone?

Important to note is that PHP will continue executing after the first case is matched. Usually, we don't want that, so we add a break statement after each case block. The break statement will jump us out of the switch statement.

Next we'll get really freaky with arrays.

Other Examples

This is the most common condition the IF statement. Let's do an actual example.

http://codebabes.com/courses/php-virg...

Control Structures. So far our PHP is pretty linear. But one of the main concepts in programming is making choices. If something happens, then do this. The if then statement is a building block for most programs. Choices require control structures, which can be broken down into conditional statements and loops. Conditionals execute a given block of code only if a certain statement is met, or TRUE. Loops, just keep looping, they execute code until a certain criteria is met.

http://codebabes.com/courses/php-virg...


In questa pagina del sito puoi guardare il video online Conditional Statements in PHP - PHP Virgin della durata di ore minuti seconda in buona qualità , che l'utente ha caricato CodeBabes 15 aprile 2014, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 70,243 volte e gli è piaciuto 194 spettatori. Buona visione!