Variables

This is PHP, and we want to be dynamic. What’s more dynamic than a variable? Nothing. I know we are all still a little scared of variables from our crazy high school math teachers, but in reality, they are possibly your greatest asset in PHP. However, PHP variables have a few strict rules, which in my opinion is the best way to have them.

Variable Rules

  1. All variables must start with a $
  2. All variables must only contain alpha-numeric characters with the only exception of _ (an underscore)
  3. All variables must start with a lowercase letter or an underscore.
  4. All variables are case sensitive, meaning that x and X are different variables

Unlike other languages, the syntax of creating a variable is remarkably simple. You don’t have to put a type keyword in front of the variable name in order to declare it.

Example

You just created the variable $myVar with a value of the string “Greetings”. PHP is smart enough to recognize what type of variable you are trying to define, well, kind of. We will get into more complex variables later. Let me show you how smart PHP is with variables really quick.

Example

Result

11

Don’t flip out. I know, I’ll explain it in a second. First, we print out $myVar that gives us the integer 1. Next, we print out $myVar2, which returns the boolean true (1 is equivalent to true in the boolean type). Also, notice that after the echo, we didn’t use quotations around our variable. With echo in PHP, you don’t use quotations around the variables. You only use quotations to tell PHP that something is a string.

Variable Scope

Don’t paint yourself in a corner. Understand scope before drafting your code full of variables.

Example

Result

Greetings

I know you might not understand what a function is, but we will get to functions later. For now, just understand that the variables in a function are segregated from the rest of the code, unless you override it, which I will never show you how to do. It’s terrible practice. Back to the example, why didn’t it echo “No Greetings”? It is because the $myVar = “No Greetings”; is confined inside of the sampleFunction‘s scope. This may be a pain sometimes, but the scope is essential in writing solid code. We don’t want variables overriding everything we type. So, just be smart when creating your variables. Think about where they need to be and what they are trying to accomplish.

References

If you enjoyed this resource, support me by sharing this page with others.
Share on Facebook
Facebook
Share on Google+
Google+
Share on StumbleUpon
StumbleUpon
Digg this
Digg
Tweet about this on Twitter
Twitter
Share on Reddit
Reddit

Leave a Comment

  1. I just tested

    <code>
    <?php
    $myVar="2";
    $myVar2=true;
    echo $myVar;
    echo $myVar2;
    ?>
    </code>

    after reading example 2. Since true is boolean and is equivalent to 1, shouldn’t false be equivalent to 0, or is false not a value in this case.

    But then again false would not leave a value because the var is false correct?

  2. For the last example, I understand local and global variables from javascript; however, if I wanted to make the echo inside the function output on the screen, would it be correct if i had wrote:

    sampleFunction();

    , so that the local variable would execute and show on the screen. I will also try it in the PHP notepad.

  3. I’m absolutely new to programming, PHP, and the like…Anyways can anyone explain to me how example two equals 11, is there anything I can read more about. I have a vast understanding of algebra, but not too sure what boolean is. I really want to get in to this, and would appreciate someone’s help, thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.