Skip to content

Instantly share code, notes, and snippets.

@MasterHans
Last active October 21, 2016 09:34
Show Gist options
  • Save MasterHans/0d0cd353e581fe35af2f5b3b46dbe34a to your computer and use it in GitHub Desktop.
Save MasterHans/0d0cd353e581fe35af2f5b3b46dbe34a to your computer and use it in GitHub Desktop.
1. Comparison operators
<?php
$a = 5; // 5 as an integer
var_dump($a == 5); // compare value; return true
var_dump($a == '5'); // compare value (ignore type); return true
var_dump($a === 5); // compare type/value (integer vs. integer); return true
var_dump($a === '5'); // compare type/value (integer vs. string); return false
//Equality comparisons
if (strpos('testing', 'test')) { // 'test' is found at position 0, which is interpreted as the boolean 'false'
// code...
}
// vs. strict comparisons
if (strpos('testing', 'test') !== false) { // true, as strict comparison was made (0 !== false)
// code...
}
3. Case
<?php
$answer = test(2); // the code from both 'case 2' and 'case 3' will be implemented
function test($a)
{
switch ($a) {
case 1:
// code...
break; // break is used to end the switch statement
case 2:
// code... // with no break, comparison will continue to 'case 3'
case 3:
// code...
return $result; // within a function, 'return' will end the function
default:
// code...
return $error;
}
}
4. Global namespace
When using namespaces, you may find that internal functions are hidden by functions you wrote.
To fix this, refer to the global function by using a backslash before the function name.
<?php
namespace phptherightway;
function fopen()
{
$file = \fopen(); // Our function name is the same as an internal function.
// Execute the function from the global space by adding '\'.
}
function array()
{
$iterator = new \ArrayIterator(); // ArrayIterator is an internal class. Using its name without a backslash
// will attempt to resolve it within your namespace.
}
5. Concatenation
<?php
$a = 'Multi-line example'; // concatenating assignment operator (.=)
$a .= "\n";
$a .= 'of what not to do';
// vs
$a = 'Multi-line example' // concatenation operator (.)
. "\n" // indenting new lines
. 'of what to do';
6. Variable declarations
At times, coders attempt to make their code “cleaner” by declaring predefined variables
with a different name. What this does in reality is to double the memory consumption of said script.
For the example below, let us say an example string of text contains 1MB worth of data,
by copying the variable you’ve increased the scripts execution to 2MB.
<?php
$about = 'A very long string of text'; // uses 2MB memory
echo $about;
// vs
echo 'A very long string of text'; // uses 1MB memory
7. Ternary operators
<?php
$a = 5;
echo ($a == 5) ? return true : return false; // this example will output an error
// vs
$a = 5;
return ($a == 5) ? 'yay' : 'nope'; // this example will return 'yay'
8. Newdoc
$str = <<<'EOD' // initialized by <<<
Example of string
spanning multiple lines
using nowdoc syntax.
$a does not parse.
EOD; // closing 'EOD' must be on it's own line, and to the left most point
<?php
return ($a == 3 && $b == 4) && $c == 5;
9. HereDoc
<?php
$a = 'Variables';
$str = <<<EOD // initialized by <<<
Example of string
spanning multiple lines
using heredoc syntax.
$a are parsed.
EOD; // closing 'EOD' must be on it's own line, and to the left most point
/**
* Output:
*
* Example of string
* spanning multiple lines
* using heredoc syntax.
* Variables are parsed.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment