Created
April 20, 2017 07:51
-
-
Save suriyadeepan/22e44b09ebf46942a87fc9285c0ff221 to your computer and use it in GitHub Desktop.
Basics of PHP
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
######## | |
# ARRAYS | |
<?php | |
$marr = array('helga', 'rollo', 'ivar'); | |
$i = 0; | |
while($i < 3){ | |
$j = 0; | |
while($j < $i){ | |
echo "\t"; | |
$j++; | |
} | |
echo "[$i] $marr[$i]\n"; | |
$i++; | |
} | |
$marr[3] = 'sigi'; | |
$marr[50] = 'athelwulf'; | |
foreach($marr as $elem){ | |
echo $elem . "\n"; | |
} | |
?> | |
############ | |
# CONSTANTS | |
<?php | |
define('PI', 3.14); | |
echo "The value of pi is " . PI . "\n"; | |
?> | |
############ | |
# Dictionary | |
<?php | |
$mdict = array('one' => 1, 'two' => 2, 3 => 'three'); | |
foreach ($mdict as $k => $v){ | |
echo "$k : $v\n"; | |
} | |
?> | |
########## | |
# FOR LOOP | |
<?php | |
$num = 0; | |
for($num=1; $num<=20; $num+=1.2){ | |
echo $num; | |
if ($num < 20){ | |
echo ", "; | |
} | |
} | |
?> | |
########## | |
# FUNCTION | |
<?php | |
function add($a, $b){ | |
return $a + $b; | |
} | |
echo "Call Function : " . add(1.6, 3.99999) . "\n"; | |
?> | |
###################### | |
# HELLO WORLD | |
<?php | |
$x = "hello world"; | |
echo "$x\n"; | |
?> | |
########### | |
# IF...ELSE | |
<?php | |
define('A', 5); | |
$a = 5; | |
if ($a > A){ | |
echo '$a is greater than A by ' . ($a - A); | |
} elseif ($a < A){ | |
echo '$a is lesser than A by ' . (-$a + A); | |
} else{ | |
echo '$a and A are equal.'; | |
} | |
if ($a == A){ | |
echo "\n" . 'a == A'; | |
} | |
$b = 5; | |
if ( ($a > $b) || ($a == A) ){ | |
echo "\n" . 'a = A or a > b'; | |
} | |
if ($a == $b){ | |
echo "\n" . 'a == b'; | |
} | |
if (! ($a > $b) ){ | |
echo "\n" . 'a !> b'; | |
} | |
?> | |
############## | |
<?php | |
echo "cool"; | |
?> | |
####################### | |
# Reference to variable | |
<?php | |
$x = 2.7; | |
$ref_to_x = &$x; | |
echo "ref_to_x : $ref_to_x\n"; | |
$x++; | |
echo "ref_to_x : $ref_to_x\n"; | |
?> | |
###################### | |
# Arithmetic shorthand | |
<?php | |
$x = 1.2; | |
$x += 0.1; | |
echo "[1] updated x : $x\n"; | |
echo "[2] updated x : " . $x += 0.1; | |
echo "\n"; | |
echo "[3] another x : " . $x++ . "\n"; | |
echo "[4] another x : " . ++$x . "\n"; | |
?> | |
################### | |
# String Operations | |
<?php | |
$mstr = ' My Special Variable '; | |
echo "Len : " . strlen($mstr) . "\n"; | |
echo "ltrim : |" . ltrim("$mstr") . "|\n"; | |
echo "rtrim : |" . rtrim("$mstr") . "|\n"; | |
echo "trim : |" . trim("$mstr") . "|\n"; | |
echo "upper : |" . strtoupper("$mstr") . "|\n"; | |
echo "lower : |" . strtolower("$mstr") . "|\n"; | |
$mstr_to_arr = explode(' ', $mstr); | |
echo "string to array :\n"; | |
echo "["; | |
foreach( $mstr_to_arr as $elem){ | |
echo "$elem ,"; | |
} | |
echo "]\n"; | |
$arr_to_mstr = implode(' ', $mstr_to_arr); | |
echo "array to string : $arr_to_mstr\n"; | |
echo "substring \"var\" in mstr : " . substr($arr_to_mstr, 13, 3) . "\n"; | |
echo "string compare : " . strcmp($arr_to_mstr, " " + $mstr) . "\n"; | |
echo "Everything after \"var\" : " . strstr($mstr, "Var") . "|\n"; | |
echo "Everything after \"var\" (case insensitive): " . stristr($mstr, "var") . "|\n"; | |
echo "Location of match \"var\" (case insensitive): " . stripos($mstr, "var") . "\n"; | |
echo "String replace : " . str_replace('Var', 'Rav', $mstr); | |
?> | |
########### | |
# Switch | |
<?php | |
$namev = "helgi"; | |
switch($namev){ | |
case "helga": | |
echo "hi helga\n"; | |
break; | |
case "rollo": | |
echo "hi rollo\n"; | |
break; | |
case "rickety": | |
echo "hi rick\n"; | |
break; | |
default: | |
echo "Who the hell are you?\n"; | |
} | |
?> | |
################## | |
# Ternary Operator | |
<?php | |
$max_num = (14 > 7)? 14 : 7; | |
echo $max_num . "\n"; | |
?> | |
################ | |
# TYPE CASTING | |
<?php | |
$x = 8.3; | |
echo "int(x) : " . (integer)$x . "\n"; | |
?> | |
####### | |
# WHILE | |
<?php | |
$num = 10; | |
while($num < 45){ | |
echo $num . ", "; | |
$num++; | |
} | |
echo "\n"; | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment