PHP Cheat Sheet 🎯

beginner
19 min

PHP Cheat Sheet 🎯

Welcome to PHP, a powerful server-side scripting language that's easy to learn and widely used for web development! In this tutorial, we'll cover the basics and some advanced concepts to help you get started with PHP. Let's dive in!

Getting Started πŸ’‘

To start using PHP, you'll need a web server like Apache or Nginx, and a PHP interpreter. Most hosting providers offer PHP out-of-the-box, or you can install it locally on your computer.

PHP Files

PHP scripts are typically saved with the .php extension. Here's a simple example of a PHP script:

php
<?php echo "Hello, World!"; ?>

πŸ“ Note: The <?php and ?> tags are used to denote the start and end of a PHP script.

Variables πŸ“

Variables in PHP store data. You can create a variable by using the $ symbol followed by the name of the variable:

php
$name = "John Doe"; $age = 25;

πŸ’‘ Pro Tip: PHP is case-sensitive. $name is different from $Name or $NAME.

Data Types 🎯

PHP has several data types, including:

  • Integer: Whole numbers, e.g., $num = 10;
  • Float: Decimal numbers, e.g., $decimal = 10.5;
  • Boolean: True or false values, e.g., $bool = true;
  • String: Text, e.g., $text = "Hello";
  • Array: A collection of values, e.g., $array = array("apple", "banana", "cherry");

Operators πŸ’‘

Operators in PHP are used to perform calculations and compare values. Here are some common operators:

  • Arithmetic: +, -, *, /, and %
  • Comparison: ==, !=, <, >, <=, and >=
  • Assignment: =
Quick Quiz
Question 1 of 1

What is the correct syntax for creating a variable in PHP?

Control Structures πŸ’‘

Control structures help you control the flow of your code. Here are some common control structures in PHP:

  • If-Else Statement:
php
if ($age >= 18) { echo "You are an adult."; } else { echo "You are a minor."; }
  • Loops:
php
for ($i = 0; $i < 10; $i++) { echo $i . " "; } echo "<br>"; foreach ($array as $value) { echo $value . " "; }

Functions πŸ’‘

Functions are reusable pieces of code that perform specific tasks. Here's an example of a simple function:

php
function greet($name) { echo "Hello, " . $name . "!"; } greet("John Doe");

Quiz Time 🎯

Now that you've learned the basics, let's test your knowledge with a quiz:

Quick Quiz
Question 1 of 1

What does the `$` symbol represent in PHP?

Quick Quiz
Question 1 of 1

What is the purpose of the `if` statement in PHP?

Good luck, and happy coding! πŸ’‘πŸ’»πŸš€