Skip to content

Instantly share code, notes, and snippets.

@simongcc
Last active August 10, 2021 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simongcc/8632996 to your computer and use it in GitHub Desktop.
Save simongcc/8632996 to your computer and use it in GitHub Desktop.
PHP useful techniques
// foreach by default is using value after "as"
foreach( $num_posts as $value ):
// echo $value."<br>";
endforeach;
// using foreach with key instead of value
foreach( array_keys( $num_posts ) as $key ):
// echo $key."<br>";
endforeach;
// to do something specific to the key may use
// Reference: http://stackoverflow.com/questions/7225070/php-array-delete-by-value-not-key
foreach( array_keys( $num_posts ) as $key ):
// echo $key."<br>";
if(($key = array_search($del_val, $num_posts)) !== false) {
unset($num_posts[$key]);
}
endforeach;
// force to display errors
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment