Home - martindubenet/PHP GitHub Wiki

wiki tab PHP

autoportrait
Welcome to my shared personnal notes. Navigate the sidebar menu to find all my precious documents. Search for keywords.

 

My PHP cheatsheets

  1. Data types
  2. Logical expressions
  3. Loops
  4. Functions
  5. Error debugging

PHP 101

<?php 
phpinfo();

Echo variables

description simple string concatenated strings contained in HTML markup
Official <?php echo $string ?> <?php echo '<p>'. $paragraph1 .'</p><p>'. $paragraph2 .'</p>' ?>
Shorthand <?= $string ?> <?= '<p>'. $paragraph1 .'</p><p>'. $paragraph2 .'</p>' ?>

Includes

For including common blocks of DOM in multiple pages

method description function
<?php include( '_example.php' ); ?>
<?php include_once( '_example.php' ); ?>
Any file include, include_once
<?php require( '_example.php' ); ?>
<?php require_once( '_example.php' ); ?>
Mandatory file require, require_once

To include relative paths is usually rely on my $rootPath = ''; variable that I declare in a globe file at the root of a project. This file is then included in the first line of all the php files. At the second line I (re)declare the variable, allowing me to specified the repository levels where that file is located. Example $rootPath = '../../';. Then I can use it in the DOM as <?php include_once( '{$basePath}inc/_example.php' ); ?>. Or I can rely on the PHP dirname function to position relatively to the parent repository of that file like <?php include_once( dirname(__FILE__) . '/inc/_example.php' ); ?>.

Concatinating strings

NOTE: Look for the “dot+equal” .= in the code below.

// original seperated strings as variables
$first_part	= "The quick brown fox";
$last_part	= " jumped over the lazy dog.";

// basic rendering of the variables (without concatenating them)
$first_part . $last_part

// concatenating the 2 variables into one string
$concatenated_parts = $first_part;
$concatenated_parts .= $last_part;
$string = $concatenated_parts;

The above concatenated $string = « The quick brown fox jumped over the lazy dog. »

Styling above string: Upper VS lower case

Note that this level of styling is relevant for backend stuff, NOT frontend where the styling is CSS {text-transform:uppercase;}.

  1. Lowercase: echo strtolower($string)        = « the quick brown fox jumped over the lazy dog. »
  2. Lowercase: echo strtoupper($string)        = « THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG. »
  3. Uppercase first: echo ucfirst($string) = « The quick brown fox jumped over the lazy dog. »
  4. Uppercase words: echo ucwords($string) = « The Quick Brown Fox Jumped Over The Lazy Dog. »

Boleans: 0 vs 1

$var1 	= true;
$var2 	= false;
$var3 	= null;
$var4 	= '';

Note that $var2 $var3 and $var4 are all 3 negative results has they are NOT TRUE but $var4 differs since it is empty.

  • echo is_null($var3); = 1
  • echo is_null($var4); = 0
  • echo empty($var4); = 1
  • echo isset($var4); = 0

List of valid PHP variable types

$jugg_var1 = '2';			// string
$jugg_var2 = 22;			// integer (int)
$jugg_var3 = 3.14;			// float
$jugg_var4 = array(3,9,27);		// array
$jugg_var5 = true;			// boolean (bool)

Vars VS Constants

Just remember that CONSTANT requires uppercase and are fixed values that you can NOT redefine like a variable can.

 

 

⚠️ **GitHub.com Fallback** ⚠️