Skip to content

Instantly share code, notes, and snippets.

<?php
/**
* Variable Scope Example #1:
*
* $temp = preceding text.
* notice DECLARED var name $temp and var passed into somedate() don't have to match
* but RETURNED var name and passed var must match
**/
$temp = "Ex. #1: The date is "; // set outside function
/**
* Static variables keep their value after function calls.
* useful for making counters to track number of function calls.
* bc $count has already been declared, initialization is skipped.
* it retains previous value, then is incremented and is displayed w new value.
*
* can only be assigned w predetermined value.
* cannot assign the result of an expression.
*/
function test() {
/**
* Loads of useful info here: https://www.php.net/reserved.variables.server
*
* Double check: always sanitize superglobals before using.
*
* ex. $came_from = htmlentitles( $_SERVER[ 'HTTP_REFERER' ] );
* ex. echo "My username is " . $_ENV[ 'USER' ];
*
* lists all server keys and values
*/
@LGitHub-sprout
LGitHub-sprout / php_ifelse_loop_withsprintf.php
Created November 9, 2020 21:11
if/else loop and sprintf
function bank_balance( $checking_balance ) {
$savings_balance = 100;
if ( $checking_balance < 100 ) {
$money = 1000;
$checking_balance += $money;
} else {
@LGitHub-sprout
LGitHub-sprout / switch.php
Created November 16, 2020 20:04
Non-looping conditionals: switch
/**
* Control structures: switch
*
* O'Reilly book: useful when on $var or result of expression can have multiple values.
*
* intersting examples in user notes
* https://www.php.net/manual/en/control-structures.switch.php
*/
// $foo = "is not set";
@LGitHub-sprout
LGitHub-sprout / ternary.php
Last active December 9, 2020 19:27
Non-looping conditionals: ternary ?
/**
* O'Reilly p.82 Expressions and Control Flow
* evaluates expression and
* executes two stmts: one when expression is T and one when F
*
* 1st stmt: when expression is true
* 2nd stmt: when expression if false
*/
$fuel = 1.00000000000001;
echo $fuel <= 1 ? "<p>Better stop for gas</p>" : "<p>Keep driving</p>";
@LGitHub-sprout
LGitHub-sprout / looping.php
Last active March 10, 2021 21:19
while, do .. while, for Loops
<?php
/**
* O'Reilly p.87
* while vs. for loop:
* use while when condition DOES NOT depend on a simple, regular change to a var.
* use while: when checking for SPECIAL INPUT OR ERROR and end loop when it occurs.
*/
$count2 = 0;
while ( $count2++ <= 12) {
echo "<p>$count2 times 12 is " . $count2 * 12 . "</p>";
@LGitHub-sprout
LGitHub-sprout / wp-config.php
Created December 15, 2020 19:23
WP debug settings
//Add detailed debug settings
define( 'WP_DEBUG', true ); //Turn on the debug engine
define( 'WP_DISABLE_FATAL_ERROR_HANDLER', true ); // Deactivate new WSOD protection
define( 'WP_DEBUG_LOG', true ); // error_log() will write to wp-content/debug.log
define( 'WP_DEBUG_DISPLAY', false ); // Write notices/warnings/errors to debug.log instead of the html
define( 'SCRIPT_DEBUG', true ); // Force WP to use dev/unminified versions of core CSS/JS
@ini_set( 'display_errors', 0 ); //Do not print error messages to screen.
@LGitHub-sprout
LGitHub-sprout / theme-functions.php
Created February 10, 2021 11:23
Replicating GeneratePress 'do_element_classes( )' function
<?php
/**
* Display HTML classes for elements
*
* @since 0.1
*
* @package wphierarchy
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
@LGitHub-sprout
LGitHub-sprout / markup.php
Created February 10, 2021 11:27
Replicating GeneratePress generate_header_classes( ) function
<?php
/**
* Adds HTML markup.
*
* @package wphierarchy
*
* @since 0.1
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.