Introduction Flashcards
creating variables and functions - basics
how to print
echo “string”;
new line
echo “\n new line”;
create a variable
$variable_name = "duck"; $variable_number = 103; $variable_float = -349.4;
combine string
echo “string one “ . “ string two”;
print string and variable
echo “$variable_name is cool”;
string and variable but adding “s or ing”
echo “${variable_name}s are cool”;
append more to a string variable
$variable_name .= “ quack”;
create variable copy
$variable_copy = $variable_name;
** is a separate variable and will stay the same even if $variable_name gets changed later
create variable alias
$variable_alias &= $variable_name;
** will always point to $variable_name, even if it is changed
math
\+ addition - subtraction * multiplication / division ** exponential % modulo (remainder)
math short syntax
Operation: Long Syntax: Short Syntax: Add $x = $x + $y $x += $y Subtract $x = $x - $y $x -= $y Multiply $x = $x * $y $x *= $y Divide $x = $x / $y $x /= $y Mod $x = $x % $y $x %= $y
increment operators
add $var++;
sub $var–;
call multiple functions
echo first() . second() . third();
function to make permanent changes to a variable
function addXPermanently(&$param){ $param = $param . "X"; return $param; } $word = "Hello"; addXPermanently($word); // Prints: HelloX echo $word; // Prints: HelloX
** add & to variable
access a global parameter in a function
$param = 10; function findParam(){ global $param; return $param * 2; }