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
- All variables must start with a $
- All variables must only contain alpha-numeric characters with the only exception of _ (an underscore)
- All variables must start with a lowercase letter or an underscore.
- 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.
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.
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.
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 have problem use gettype() and settype()
Some time have problem.
If you waht work with gettype function for work with variable
http://us2.php.net/manual/ru/function.gettype.php
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!
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.
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?