Skip to content

Instantly share code, notes, and snippets.

@armanhakimsagar
Last active February 13, 2020 10:14
Show Gist options
  • Save armanhakimsagar/b2c3076a30f61efabe57ca08e0b7da8b to your computer and use it in GitHub Desktop.
Save armanhakimsagar/b2c3076a30f61efabe57ca08e0b7da8b to your computer and use it in GitHub Desktop.
PHP Basic:
1. echo 0x8A + 3
2. true ? 0 : true ? 1 : 2;
3. PHP code (or \r, \n, \r\n)
4. echo ~ $x & $y;
5. echo $x << $y;
6. echo $x >> $y;
7. echo $x ^ $y;
8. (5 % -3) = 2; (-5 % 3) = -2
9. $a = ($b = 4) + 5;
10. 3 = $a; // output error
11. $a = 3; $b = &$a; $b = 50;print "$a\n"; print "$b\n";
12. $a = &new C;
13. print 0=='a1'? 'TRUE': 'FALSE';
14. 0=='a1'? print 'TRUE': print 'FALSE';
15. NOT 0=='a1'? echo 'TRUE': echo 'FALSE';
16. Execution (backtick) Operators
17. Error Control Operators
18. if ($obj instanceof A)
19. $greeting = <<<"GREETING" She said "That is $name's" dog! GREETING;
20. $greeting = <<<'GREETING' She said "That is $name's" dog! GREETING
21. var_dump(01090);
22. 0b , 0x
23. 1.234, 1.2e3, 7E-10
24. 000 is TRUE
25. "Hello ".$arr['fruit'];
26. "Hello {$arr['fruit']}"
27. function increment(&$var){ call_user_func();++$var;}
$a = 0;
call_user_func('increment', $a);
echo $a;
28. $a = 'name';
$$a = "Paul";
echo ${'name'}; //Paul
29. function myfunc() { … } $f = 'myfunc'; $f();
30. function SUM() { global $a, $b; return $a + $b; }
31. function SUM() { return $GLOBALS['a'] + $GLOBALS['b']; }
32. function SUM($b) { static $a = 0; $a = $a + $b; return $a; }
33. PHP_EOL
34. switch ($i) {
case 0: // a semi-colon may be used i.e. case 0;
case 1:
case 2:
echo "i is less than 3 but not negative";
break;
case 3:
echo "i is 3";
}
35.
for($i=0,$j=50; $i<100; $i++) {
while($j--) {
if($j==17) goto end;
}
}
echo "i = $i";
end:
echo 'j hit 17';
36.
print 'something'; or print ('something');
die; die(); or exit; exit();
return; // halt the execution and return a value, code after which will NOT run but parsed
__halt_compiler(); // useful for debugging, code after which will not be parsed
Evaluation Language Constructs
empty(); emptyeval(); require(); or include_once/require_once();
reset()
37. set_error_handler
38. try { } catch (Exception $e) { echo $e->getMessage(); }
39. difference between $a=’\my\username’; $a=“\\my\\username”;
40.
function foo(&$var){
$var++;
};
$a=5;
foo($a);
41. PECL (PHP Extension Community Library)
42. gc_collect_cycles()
43.
$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
44.
$x = 8 - 6.4; // which is equal to 1.6
$y = 1.6;
var_dump($x == $y);
45.
$strHex = "C45F82ED";
$bin = str_pad(base_convert($strHex, 16, 2), 32, "0", STR_PAD_LEFT);
$sign = $bin[0];
$exp = bindec(substr($bin, 1, 8)) - 127;
$man = (2 << 22) + bindec(substr($bin, 9, 23));
$dec = $man * pow(2, $exp - 23) * ($sign ? -1 : 1);
echo "Answer = " . $dec . "<BR>\n";
46. echo (4.1-floor(4.1));
47.
$x = 0.23;
$js = "var foo = doBar($x);";
print $js;
48.
function strrev_x($s, $x = 2) {
if ($x <= 1) {
return strrev($s);
} else {
return (implode(array_reverse(array_map('implode', array_chunk(str_split($s), $x)))));
}
}
49.
$tst = pack('d', '1.6');
var_dump(strrev_x(bin2hex($tst)));
$tst = pack('d', 8-6.4);
var_dump(strrev_x(bin2hex($tst)));
echo 'float pack'. PHP_EOL;
$tst = pack('f', '1.6');
var_dump(strrev_x(bin2hex($tst)));
$tst = pack('f', 8-6.4);
var_dump(strrev_x(bin2hex($tst)));
50.
function hexTo32Float($strHex) {
$v = hexdec($strHex);
$x = ($v & ((1 << 23) - 1)) + (1 << 23) * ($v >> 31 | 1);
$exp = ($v >> 23 & 0xFF) - 127;
return $x * pow(2, $exp - 23);
}
echo hexTo32Float("C4028000"); // outputs: -522
echo hexTo32Float("457F9000"); // outputs: 4089
echo hexTo32Float("2D7F5"); // outputs: 6.00804264307E-39
echo hexTo32Float("0002D7F5"); // outputs: 6.00804264307E-39
echo hexTo32Float("47D9F95E"); // outputs: 111602.734375
51.
$binarydata32 = pack('H*','00000000');
$float32 = unpack("f", $binarydata32); // 0.0
$binarydata64 = pack('H*','0000000000000000');
$float64 = unpack("d", $binarydata64); // 0.0
52.
for( $tmp = 0, $i = 0; $i < 100; $i++ ) {
$tmp += 100000;
echo round($tmp),"\n";
}
53. echo intval(19.31 * 100);
54.
function hex2float($number) {
$binfinal = sprintf("%032b",hexdec($number));
$sign = substr($binfinal, 0, 1);
$exp = substr($binfinal, 1, 8);
$mantissa = "1".substr($binfinal, 9);
$mantissa = str_split($mantissa);
$exp = bindec($exp)-127;
$significand=0;
for ($i = 0; $i < 24; $i++) {
$significand += (1 / pow(2,$i))*$mantissa[$i];
}
return $significand * pow(2,$exp) * ($sign*-2+1);
}
55.
<?php if ($expression == true): ?>
This will show if the expression is true.
<?php else: ?>
Otherwise this will show.
<?php endif; ?>
56.
<?php
$me = 'Pyornide';
?>
<?=$me;?> is happy.
<?php
$me = strtoupper($me);
?>
<?=$me;?> is happier.
57.
function long_to_GET(){
/**
* This function converts info.php/a/1/b/2/c?d=4 TO
* Array ( [d] => 4 [a] => 1 [b] => 2 [c] => )
**/
if(isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] != ''){
//Split it out.
$tmp = explode('/',$_SERVER['PATH_INFO']);
//Remove first empty item
unset($tmp[0]);
//Loop through and apend it into the $_GET superglobal.
for($i=1;$i<=count($tmp);$i+=2){ $_GET[$tmp[$i]] = $tmp[$i+1];}
}
}
58.
$_GET['avar'] = 'b';
print_r($_GET); print('<br>');
print_r($_REQUEST);
59.
var_dump(isset($_ENV['PATH'])); // bool(false)
getenv('PATH');
var_dump(isset($_ENV['PATH'])); // bool(true)
60.
$a = 1;
$b = 2;
function Sum()
{
$GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}
Sum();
echo $b;
61.
static $int = 0; // correct
static $int = 1+2; // correct (as of PHP 5.6)
static $int = sqrt(121); // wrong (as it is a function)
$int++;
echo $int;
62.
for($j=0; $j<3; $j++)
{
if($j == 1)
$a = 4;
}
echo $a;
63.
function foo ()
{
global $testvar;
$localvar = new Object ();
$testvar = &$localvar;
}
foo ();
print_r ($testvar);
64.
if (isset($_COOKIE['count'])) {
$count = $_COOKIE['count'] + 1;
} else {
$count = 1;
}
setcookie('count', $count, time()+3600);
setcookie("Cart[$count]", $item, time()+3600);
65.
function multi_post_item($input_item_name) {
$array_output = array();
$in_handle = fopen("php://input", "r");
$raw_input_items = split("&", urldecode(fread($in_handle, 100000)));
foreach ($raw_input_items as $input_item) {
// split this item into name/value pair
$item = split("=", $input_item);
// form item name
$item_name = $item[0];
// form item value
$item_value = $item[1];
if ($item_name == $input_item_name) {
$array_output[] = $item_value;
}
}
return $array_output;
}
66.
echo "$a ${$a}";
67.
$Bar = "a";
$Foo = "Bar";
$World = "Foo";
$Hello = "World";
$a = "Hello";
$a; //Returns Hello
$$a; //Returns World
$$$a; //Returns Foo
$$$$a; //Returns Bar
$$$$$a; //Returns a
$$$$$$a; //Returns Hello
$$$$$$$a; //Returns World
68.
$tab = array("one", "two", "three") ;
$a = "tab" ;
${$a}[] = "four" ; // <==== this is the correct way to do it
print_r($tab) ;
69.
${date("M")} = "Worked";
echo ${date("M")};
70.
namespace MyNamespace;
class Foo
{
public function __construct()
{
echo "I'm a real class!" . PHP_EOL;
}
}
$class = 'MyNamespace\Foo';
$instance = new $class;
71.
class pp{
var $prop1=1,$prop2=2,$prop3=array(3,4,5);
function fun1(){
$vars=get_class_vars('pp');
while(list($var,$value)=each($vars)){
$ref=& $this->$var;
$ref=$_GET[$var];
} // while
var_dump($this);
}
}
$_GET['prop1']="uno";
$_GET['prop2']="dos";
$_GET['prop3']=array('tres','cuatro','cinco','seis');
$p=new pp();
$p->fun1();
72.
$p1=microtime(true);
$x=0;
for($i=0;$i<50000000;$i++) {
$x+=CNS;
}
$p2=microtime(true);
73.
define('echo', 'My constant value');
echo constant('echo'); // outputs 'My constant value'
74.
function double($i)
{
return $i*2;
}
$b = $a = 5; /* assign the value five into the variable $a and $b */
$c = $a++; /* post-increment, assign original value of $a
(5) to $c */
$e = $d = ++$b; /* pre-increment, assign the incremented value of
$b (6) to $d and $e */
/* at this point, both $d and $e are equal to 6 */
$f = double($d++); /* assign twice the value of $d before
the increment, 2*6 = 12 to $f */
$g = double(++$e); /* assign twice the value of $e after
the increment, 2*7 = 14 to $g */
$h = $g += 10; /* first, $g is incremented by 10 and ends with the
value of 24. the value of the assignment (24) is
then assigned into $h, and $h ends with the value
of 24 as well. */
75.
$a = 2, $b = 4;
echo $a."\n";
echo $b."\n";
Example (works):
for ($a = 2, $b = 4; $a < 3; $a++)
{
echo $a."\n";
echo $b."\n";
}
76.
$n = 3;
$n * --$n
77.
function a() {echo 'a';}
function b() {echo 'b';}
a() == b(); // outputs "ab", ie evaluates left-to-right
$a = 3;
var_dump( $a == $a = 4 );
78.
print array() == NULL ? "True" : "False";
print " (" . (array() == NULL) . ")\n";
$arr = array();
print array() == $arr ? "True" : "False";
print " (" . (array() == $arr) . ")\n";
print count(array()) . "\n";
print count(NULL) . "\n";
79.
$a = 2;
echo ($a == 1 ? 'one' :
($a == 2 ? 'two' :
($a == 3 ? 'three' :
($a == 4 ? 'four' : 'other') ) ) );
echo "\n";
80.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment