PHP Cheat Sheet

Gamze Yılan
Nerd For Tech
Published in
6 min readAug 10, 2021

If you’re already familiar with the programming languages and would like a quick start to PHP skipping the basic common things, this one is for you.

What is What

PHP is a scripting language that is used for web development. It’s very easy to get started with, has an active and supportive community and still a skill that is required quite a bit.

Unlike your classic html-css-js project where all your code runs inside a browser, with PHP, your code will run within the server.

Developing a PHP project comes with many other dependencies such as a database and a server on top of PHP itself. So, in order to get it all downloaded at once, we’re going to use XAMPP in this tutorial. You can get it from here.

After you’ve set up XAMPP, you can go to the XAMPP Control Panel to start any other dependencies you’re going to need. For PHP development we’re going to use Apache server and further in this tutorial, the MySQL server. As soon as you start your Apache server, you can go to the browser of your preference and type in “localhost” as the URL to get to the local Apache server. This is where you’ll host your project locally and watch for the changes.

Any project that we’d like to run via XAMPP, we must put them under XAMPP/htdocs folder. Then, we can simply start our Apache server and go to the URL localhost/projectName to see it. Doins so will, if exists, direct you to the index.php file automatically.

Getting Started

The ultimate goal of the PHP is to get dynamic data (maybe from a database or user input) and embed it into html to perform a manipulation on it.

The php code is written within the:

<?php //// your code here /// ?>

And to declare a variable, we use the dollar sign and we name them using uppercase/lowercase letters or underscores.

$name = ‘Gamze’;

Later within your html or php code, you can use this variable inside the php tags as;

<div> <?php echo $name; ?> </div>

Within your php code, you can overwrite any variables that you’ve defined before. However, if you’d like to avoid this, you can use the define function. This function takes two parameters, a variable name and a value, and then attaches the value to the variable permanently.

define (‘name’, ‘gamze’);

Alternatively, you can use two variables side by side using a dot.

$string= ‘Hello my name is:’ ;

$name = ‘Gamze’;

echo $string . $name;

Alternatively, you can use two variables together via single or double quotes;

echo ‘$string $name’;

echo “Hello I’m $name”;

PHP will see a string as an array of characters. So if you would like to get the first letter “g” of our variable, use this:

$name[0];

You can find the length of a string using the function:

strlen($name);

You can make your variable all uppercase or lowercase using the functions:

strtoupper($name);

strtolower($name);

You can add, subtract, multiply, divide and take power of with PHP using the signs +, -, *, /,**. You can, alternatively, perform a math method and equal the result to the variable simply by:

$number =40;

$number +=10; // now the number variable holds the value of 50

You can take a float number and change it to the closest lower or higher integer by using the functions;

$number = 2.17;

floor($number); //gives a result of 2

ceil($number); //gives a result of 3

Once you create an array within PHP, you can echo each item within the array via the index. Yet if you try to echo the entire array you’ll get an error. So instead, you can echo the array using the function below;

print_r($arrayName);

There are two options when it comes to adding a new element to an array:

$arrayName [1] = 60; //Will replace whatever you have within the index 1 of the array with 60. If there’s nothing there, will add a new item to the mentioned index.

array_push($arrayName, 60); //Will add the value 60 to the end of the array

Alternatively, you can add one array to the end of another one to make a new array with the merge function:

$arrayThree = array_merge ($arrayOne, $arrayTwo);

You can also create arrays that work as pairs of keys and values.

$arrayName =[‘Gamze’ => ‘Female’, ‘John’ => ‘Male’, ‘Jane’ => ‘Female’]

echo arrayName[‘John’] //Will print out ‘Male’ to the screen.

You can delete some item from an array with the pop method;

array_pop($arrayName); //Will delete the last item from the array

Loops

A for loop can be defined within PHP files as;

for ($i=0; $i<5; $i++) { };

If we’re running, let’s say, an array through a for loop we can count the items via the count function or simply use the foreach function.

for ($i=0; $i<count(someArray); $i++) { };

foreach($someArray as $i) {}; //note that you can access each item within the array with i if you use this notation.

Embedding PHP into HTML

You can write PHP inside HTML using the php tags as shown above. You can also combine PHP with HTML as:

<div> <?php foreach($products as $i) { ?>

<p> <?php echo $product[‘price’]; ?> Turkish Lira </p> </div>

Assignation vs Comparison

Assignation of a variable to another one can be mistaken with the comparison grammar we use within this language. See the examples below:

$var1=5; $var2 = 10; $var3 = ‘5’;

echo $var1 = $var2; // will assign the value 10 into var1 and print that out.

echo $var1 == $var3; // will compare if 5 and ‘5’ are the same and return true since they both hold the same value. This is called a “loose comparison”.

echo $var1 === $var3; // will return false, because this is called a “strict comparison” and although the values are the same, the two variables are not exactly the same and they are in f ct of two different data types because of the quotation marks.

Note: If PHP returns false on echo, it will print nothing on the screen, yet if it turns true it will print “1”.

Tip: If you’re running a variable within double brackets using echo, PHP will acknowledge this. However, if your variable in this case is an array that works as key-value pairs and you’re trying to call a value using the key, it’s not going to work. So in that case make sure to add curly brackets as:

echo “{$arrayName[‘key’]}”;

Declaring Global & Local Variables

PHP works with scopes. If you declare a variable inside a function, it can be used only within the function’s scope. Likewise, if you declare one outside the function, it will not read the variable within the function. If you’d like to use a variable that is declared outside the function within, use the syntax below:

$name: “Gamze”;

function sayHello () {

global $name;

echo “hello $name”; }

You can also take a global variable within a function’s scope by declaring it as a parameter when you call that function, as:

$name: “Gamze”;

function sayHello () {echo “hello $name”; }

sayHello ($name);

Include & Require

You can get the code into a .php file from another .php file using the include or the require function as:

<?php include(‘otherfile.php’); ?>

<?php require(‘otherfile.php’); ?>

The difference between two is that, both tags will run the included/required file first. But if there’s an error with the mentioned file, if it’s declared with the include tag the rest of the page will still appear yet if it’s declared with the required tag the rest of the main page will also fail to load.

Remember that as long as it’s within the php tags you can include a new file wherever you desire and as many times as you desire within your project -even in the middle of html!

--

--