Last active
November 5, 2015 22:37
-
-
Save rurtubia/18c3eed5256efc6dabcb to your computer and use it in GitHub Desktop.
PHP Variables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<HTML> | |
<BODY> | |
<?php | |
//GLOBAL VARIABLES are written outside any functions and can only be accessed outside functions | |
$vg1 = 123; | |
$vg2 = 456; | |
echo $vg1 + $vg2; | |
//LOCAL VARIABLES exist only inside a function: | |
function testFuncion(){ | |
$vl1 = 123; | |
$vl2 = 444; | |
echo $vl1 + $vl2; | |
} | |
//Calling the function: | |
testFunction(); | |
//USING A GLOBAL VARIABLE FROM A LOCAL CONTEXT | |
function testFuncion2(){ | |
$vl1 = 123; | |
# it's necessary to call the variable before using it. | |
global $vg1; | |
# after that it can be used. | |
echo $vg1 + $vl1; | |
} | |
testFunction2(); | |
//GLOBAL VARIABLES ARE STORED IN AN ARRAY CALLED $GLOBALS | |
#and they can be used and modified from within a local context | |
function testFunction3(){ | |
$GLOBALS['vg1'] = $GLOBALS['vg1'] + $GLOBALS['vg2']; | |
} | |
testFunction3(); | |
#prints the new value of vg1 | |
echo "<br>"; | |
echo $vg1; | |
?> | |
</BODY> | |
</HTML> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE HTML> | |
<HTML> | |
<BODY> | |
<?php | |
#php variables start with the $ symbol and then have a name | |
$var1 = "variable"; | |
#the variable name cannot start with a number | |
#a variable name can only contain A-z, 0-9 and _ | |
#variable names are case sensitive | |
//VARIABLE OUTPUT | |
#to output a variable, we use the echo comand | |
echo "this is a $var1!"; | |
#we can use the concatenation operator "." | |
echo "this is a " . $var1 . "!"; | |
#Math operations with variables: | |
echo "<br>"; | |
echo "The sum of 10 + 50 is "; | |
echo $x + $y; | |
?> | |
</BODY> | |
</HTML> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment