eBuzz Nigeria

PHP Cheat Sheet (.PDF Version Included)

Our PHP cheat sheet goals to assist anybody attempting to get proficient in or enhance their information of PHP. The programming language is among the many hottest in internet improvement. It’s within the coronary heart of WordPress, the world’s most popular CMS, and likewise varieties the bottom of different platforms like Joomla and Drupal. (Don’t miss our comparison of the three.)

Aside from that, PHP is an Open Source and thus free to use. Since its inception in 1995, it has had a number of releases. The latest model, PHP 7.2, got here out on the finish of 2017.

PHP is a server-side language, that means that it executes on the server, not within the consumer’s browser (as opposed to, for instance, JavaScript). PHP scripts produce HTML which is then handed on to the browser for interpretation. Consequently, the consumer doesn’t see the code itself however solely the outcome.

php cheat sheet
By GgiaEsquema-proxy-internet.svg: Randomicc [CC BY-SA 3.0], from Wikimedia Commons

The programming language is comparatively straightforward to study for learners, nevertheless it additionally provides loads of superior potentialities for veteran programmers.

For that motive, the next PHP cheat sheet is appropriate for you regardless of the place you might be in your journey. It covers an important PHP ideas and capabilities and acts as a fast reference information for these utilizing PHP for internet improvement.

We have so much to cowl, so let’s get proper into it. If that’s not sufficient for you, we even have cheat sheets for HTML, CSS, and jQuery in addition to the aforementioned JavaScript.

PHP Cheat Sheet

PHP Cheat Sheet – The Basics

We are beginning off with the fundamentals – how to declare PHP in a file, write feedback and output information.

Including PHP in a File

PHP information finish in .php. Besides PHP itself, they will comprise text, HTML, CSS and JavaScript. In order for a browser to acknowledge PHP, you want to wrap it in brackets: <?php and ?>. Consequently, you possibly can execute PHP in a web page:

  // place PHP code right here

Writing Comments

Like many different languages, PHP additionally has the flexibility to add feedback. This is essential for annotating your code for human readers however in a manner that the browser doesn’t attempt to execute it. In PHP, you might have a number of methods for that:

A standard instance of using feedback are WordPress theme headers:

  Theme Name: Twenty Seventeen
  Theme URI: https://wordpress.org/themes/twentyseventeen/
  Author: the WordPress crew
  Author URI: https://wordpress.org/
  Description: Twenty Seventeen brings your website to life with header video and immersive featured photos. With a give attention to business websites, it options a number of sections on the entrance web page in addition to widgets, navigation and social menus, a emblem, and extra. Personalize its asymmetrical grid with a {custom} colour scheme and showcase your multimedia content material with put up codecs. Our default theme for 2017 works nice in lots of languages, for any talents, and on any machine.
  Version: 1.5
  License: GNU General Public License v2 or later
  License URI: https://www.gnu.org/licenses/gpl-2.0.html
  Text Domain: twentyseventeen
  Tags: one-column, two-columns, right-sidebar, flexible-header, accessibility-ready, custom-colors, custom-header, custom-menu, custom-logo, editor-style, featured-images, footer-widgets, post-formats, rtl-language-support, sticky-post, theme-options, threaded-comments, translation-ready
  This theme, like WordPress, is licensed below the GPL.
  Use it to make one thing cool, have enjoyable, and share what you have discovered with others.

Outputting Data

In PHP, information is often output utilizing echo or print. For instance, the title of this weblog put up is perhaps displayed on a web page like this:

  echo "<h1>PHP Cheat Sheet</h1>";

The two instructions echo and print are just about the identical. The solely difference is that the previous has no return worth and may take a number of parameters, whereas the latter has a return worth of 1 and may solely take one argument.

An essential word: Like all different PHP instructions, capabilities echo and print will not be case delicate. That signifies that whenever you write ECHO, EcHo, eCHO or every other variation, they may proceed to work. As you'll study additional on, that doesn’t apply to every thing.

Writing PHP Functions

Functions are shortcuts for generally used chunks of code. They make programming a lot simpler since you don’t have to re-use lengthy code snippets. Instead, you create them as soon as and use the shortcuts whenever you want them.

It’s potential to create your personal PHP capabilities however there additionally many constructed into the programming language. Much of this PHP cheat sheet is devoted to that.

The primary syntax to create a perform:

perform NameOfThePerform() {
      //place PHP code right here

Quick clarification: the primary half is the perform a reputation (reminder: perform names will not be case delicate). After that, every thing between the curly braces is what the perform does when referred to as.

Variables and Constants

Similarly to most different programming languages, PHP allows you to work with variables and constants. These are items of code that retailer totally different sorts of knowledge.

Defining Variables

To do something with variables, you first want to outline them. In PHP, you denote a variable utilizing the $ signal and assign its worth utilizing =. A typical instance:

  $BlogPostTitle = "PHP Cheat Sheet";

A number of essential factors:

Types of Data

Variables can tackle several types of information:

There is not any want to declare PHP variables in a sure manner. They routinely tackle the kind of the info they comprise.

Variable Scope

Variables may be accessible in numerous scopes, that means the a part of a script you possibly can entry them. This may be world, native and static.

Any variable declared outside of a perform is obtainable globally. That means it may be accessed outside of a perform as properly.

If you declare a variable inside a perform, it would have a neighborhood scope. The consequence is that it might solely be accessed inside that perform.

A manner round that is to prepend a neighborhood variable with world. That manner, it turns into a part of the worldwide scope.

perform myFunction() {
      world $a, $b;
      $b = $a - $b;

In each instances, the variable turns into a part of the $GLOBALS variable talked about under.

Finally, it’s additionally potential to add static in entrance of a neighborhood variable. That manner, it received’t be deleted after its perform is executed and may be reused.

Predefined Variables

PHP additionally comes with quite a lot of default variables referred to as superglobals. That’s as a result of they're accessible from wherever, no matter scope.

Variable-handling Functions

Aside from that, there are a complete bunch of capabilities to work with variables:


Aside from variables, you too can outline constants which additionally retailer values. In distinction to variables their worth can't be modified, it’s locked in.

In PHP you possibly can outline a relentless:

outline(identify, worth, true/false)

The first is the identify, the second the fixed’s worth and the third parameter whether or not its identify ought to be case delicate (the default is false).

Constants are helpful since they permit you to change the worth for a complete script in a single place as a substitute of getting to change each occasion of it. They are additionally world in nature, that means they are often accessed from wherever.

Aside from user-defined constants, there additionally quite a lot of default PHP constants:

PHP Arrays – Grouped Values

Arrays are a manner to manage a number of values in a single variable in order that they can be utilized collectively. While capabilities are for blocks of code, arrays are for the values – a placeholder for bigger chunks of knowledge.

In PHP there are several types of arrays:

Declaring an Array in PHP

Arrays in PHP are created with the array() perform.

  $cms = array("WordPress", "Joomla", "Drupal");
  echo "What is your favorite CMS? Is it " . $cms[0] . ", " . $cms . " or " . $cms . "?";

Array keys can both be strings or integers.

Array Functions

PHP provides a large number of default capabilities for working with arrays:

PHP Strings

In programming speech strings are nothing greater than text. As we've got settled earlier, they're additionally a legitimate worth for variables.

Defining Strings

In PHP there are a number of methods to outline strings:

Note: Strings can comprise variables, arrays, and objects.

Escape Characters

String Functions

PHP Operators

Operators permit you to carry out operations with values, arrays and variables. There are a number of differing types.

Arithmetic Operators

Your customary mathematic operators.

Assignment Operators

Besides the usual task operator (=), you even have the next choices:

Comparison Operators

Logical Operators

Bitwise Operators

Error Control Operator

You can use the @ signal to forestall expressions from producing error messages. This is commonly essential for security reasons, for instance to preserve confidential info protected.

Execution Operator

PHP helps one execution operator, which is `` (backticks). These will not be single-quotes! PHP will try to execute the contents of the backticks as a shell command.

Increment/Decrement Operators

String Operators

Loops in PHP

Loops are quite common in programming. They permit you to run by way of the identical block of code below totally different circumstances. PHP has a number of totally different ones.

For Loop

This sort goes by way of a block of code a specified variety of instances:

for (beginning counter worth; ending counter worth; increment by which to improve) {
      // code to execute goes right here

Foreach Loop

A loop utilizing foreach runs by way of every ingredient in an array:

foreach ($InsertYourArrayName as $worth) {
      // code to execute goes right here

While Loop

Loops by way of a block of code so long as a specified situation is true.

whereas (situation that should apply) {
      // code to execute goes right here

Do…While Loop

The remaining PHP loop runs a code snippet as soon as, then repeats the loop so long as the given situation is true.

do {
      // code to execute goes right here;
  } whereas (situation that should apply);

Conditional Statements

If/else statements are comparable to loops. They are statements for working code solely below sure circumstances. You have a number of choices:

If Statement

Executes code if one situation is true.

if (situation) {
      // code to execute if situation is met


Runs a chunk of code if a situation is true and one other if it isn't.

if (situation) {
      // code to execute if situation is met
  } else {
      // code to execute if situation is just not met


Executes totally different code snippets for greater than two situations.

if (situation) {
      // code to execute if situation is met
  } elseif (situation) {
      // code to execute if this situation is met
  } else {
      // code to execute if not one of the situations are met

Switch Statement

Selects one among a number of blocks of code to execute.

change (n) {
      case x:
          code to execute if n=x;
      case y:
          code to execute if n=y;
      case z:
          code to execute if n=z;
      // add extra instances as wanted
          code to execute if n is neither of the above;

Working with Forms in PHP

PHP is commonly used for dealing with internet varieties. In explicit, the aforementioned $_GET and $_POST assist to acquire information despatched by way of a type. Both are in a position to catch values from enter fields, nonetheless, their utilization differs.

Using GET vs POST

GET collects information by way of URL parameters. That means all variable names and their values are contained within the web page deal with.

The benefit of that is that you simply’re in a position to bookmark the data. Keep in thoughts that it additionally signifies that the data is seen to everybody. For that motive, GET is just not appropriate for delicate info resembling passwords. It additionally limits the quantity of knowledge that may be despatched in ca 2000 characters.

POST, then again, makes use of the HTTP POST technique to go on variables. This makes the info invisible to third events, as it's despatched within the HTTP physique. You will not be in a position to bookmark it.

With POST, there aren't any limits to the quantity of knowledge you possibly can ship. Aside from that, it additionally has superior performance and is due to this fact most popular by builders.

Form Security

The most essential difficulty when it comes to internet varieties is safety. If not arrange correctly, they're susceptible to cross-scripting assaults. The hackers add scripts to unsecured internet varieties to use them for their very own function.

PHP additionally provides instruments to thwart these assaults, particularly:

You will notice that we've got encountered all of these perform within the earlier part on strings. When you embrace them within the script that collects the shape information, you possibly can successfully strip dangerous scripts of the characters they want for functioning, rendering them unusable.

Required Fields, Error Messages and Data Validation

Aside from that, PHP is ready to outline required fields (you possibly can’t submit the shape with out filling them out), show error messages if some info is lacking and to validate information. We have already talked in regards to the obligatory instruments to achieve this.

For instance, you possibly can merely outline variables to your type fields and use the empty() perform to examine if they've values. After that, create a easy if/else assertion to both ship the submitted information or output an error message.

The subsequent step is to examine the submitted information for validity. For that, PHP provides quite a lot of filters resembling FILTER_VALIDATE_EMAIL to be certain a submitted e-mail deal with has the precise format.

PHP Filters

Filters are used to validate and filter information that's coming from insecure sources. As talked about, a standard instance is consumer enter. PHP provides quite a lot of filter capabilities and constants for that:

Filter Functions

Filter Constants

HTTP Functions in PHP

PHP additionally has the performance to manipulate information despatched to the browser from the online server.

HTTP Functions

Working with MySQL

Many platforms which are primarily based on PHP work with a MySQL database within the background. For that motive, it’s essential to be acquainted with the capabilities that permit you to work with them.

MySQL Functions

Date and Time

Of course, PHP capabilities for date and time shouldn't be lacking from any PHP cheat sheet.

Date/Time Functions

Date and Time Formatting

PHP Errors

Finally, for the instances that issues don’t go easily and also you want to discover out the place the Challenge lies, PHP additionally provides performance for errors.

Error Functions

Error Constants


Knowing your manner round PHP is a good suggestion for anybody considering internet design and internet improvement. Especially if you'd like to dive deeper into the technical elements of creating your own website.

The PHP cheat sheet above gives you with an outline over some central elements of PHP. Bookmark it as a reference or use it as a springboard to study extra in regards to the programming language. We sincerely hope you might have discovered it a helpful useful resource.

If you might have any addition to the PHP cheat sheet, please don’t hesitate to tell us within the feedback part under!

Share on FacebookShare on TwitterShare on LinkedinShare on Pinterest

Search More of: , , , ,

« | »

Recent Posts