Skip to content
Sign upLog in

PHP | All You Need to Know!

Profile icon


This is the ultimate tutorial on PHP! From echoes to passwords-it has everything!
Let's get started right away!

PHP is a lot like HTML-really.
PHP is like HTML with PHP codes, more or less.
So really, PHP is just HTML with more stuff!
But... There's something bad about it... only supports PHP in PHP repls. If you create an HTML repl and add a PHP file, this happens:

That's right, folks.
PHP in an HTML repl just shows the code.
BUT, you can make a PHP repl and then add HTML files and, if you want, more PHP files.


Ok, let's get started with the basics.

The Basics

echo is probably the most common php command. echo writes that specific text to the html document.
For example:

<?php echo "This text is echoed!"; ?>

IMPORTANT NOTE: Always add ; after each PHP line of code.


Since the PHP echo tag writes on the html, you can also add html:

<? echo "I <i>love</i> PHP!" ?>


Also, the echo tag does not erase the whole document, so you can add as many echo tags as you want.

<?php echo "This text is echoed! "; echo "This text, too!"; ?>


Super cool, right?

PHP Data Types

PHP also has variables, arrays, you know, the usual stuff.
PHP variables start with $. I don't know why. Maybe the guy who made PHP is obsessed with money. But that doesn't matter right now.
Variable example:

<?php $myVariable = 0; ?>

Also, you can add strings:

<?php $myVariable = "Sting!"; ?>

Here's how you use variables:

<?php $feet = 6; echo "Stay ", $feet, " feet apart!"; ?>


Using commas joins text or number together.
Also, there is arrays.
Example of an array:

<?php $cars = array("BMW", "Toyota", "Audi"); ?>

Getting the information from arrays:

<?php $cars = array("BMW", "Toyota", "Audi"); echo "I like the car brand ", $cars[0]; ?>


And, changing the data from the array:

<?php $cars = array("BMW", "Toyota", "Audi"); $cars[1] = "Ford"; echo "I also like ", $cars[1]; ?>


PHP If, Else, Else If Statements

Now, sometimes PHP can be a lot like JavaScript.
Here's where it does.
An if statement is like this:

<?php if (5 == 7) { echo "5 is equal to 7!"; } else { echo "5 is not equal to 7!"; } ?>

If 5 is equal to 7, it will echo "5 is equal to 7!".
But, if it is not, than it will echo "5 is not equal to 7!".
And we all know 7 is not equal to 5, so we should get this:

Also, there is elseif:

<?php if (5 == 4) { echo "5 is equal to 4!"; } elseif (5 == 5) { echo "5 is not equal to 4, but it is equal to 5!"; } else { echo "5 is not equal to 4 or 5!"; } ?>


The reason is because the elseif echoes "5 is not equal to 4, but it is equal to 5!" if 5 is not equal to 4, but is equal to 5.
But let's say we changed that code to this...

<?php if (5 == 5) { echo "5 = 5"; } elseif (5 == 5) { echo " 5 = 5"; } else { echo "5 is not equal to 5"; } ?>

And we will get only one "5 = 5":

You're probably thinking:

That's so weird...
I added two if statements!

You only added one. Why? Because that one you thought is the second one is also an else. So, 5 does equal 5. But that's happening. So, since that elseif technically is an if and an else, it can't happen unless the if right next to it doesn't happen.
There's way more operators than the = sign. Look:

==Equal$x == $yReturns true if $x is equal to $y
===Identical$x === $yReturns true if $x is equal to $y, and they are the same data type
!=Not Equal$x != $yReturns true if $x is not equal to $y
<>Not Equal$x <> $yReturns true if $x is not equal to $y
!==Not Identical$x !== $yReturns true if $x is not identical to $y, or they are not the same data type
>Greater Than$x > $yReturns true if $x is greater than $y
<Less Than$x < $yReturns true if $x is less than $y
>=Greater than or Equal To$x >= $yReturns true if $x is greater than or equal to $y
<=Less than or Equal$x <= $yReturns true if $x is less than or equal to $y

PHP Logical Operators

andAnd$x and $yTrue if both $x and $y are true
orOr$x or $yTrue if either $x or $y is true
xorXor$x xor $yTrue if either $x or $y is true, but not both
From PHP Operators (

PHP Variable Changing

Changing variables is quite simple, for example:

<?php $x = 1; $x += 1; echo $x; ?>

+= adds the number to the number next to it.

Also, there are these:

  • -=: Subtraction
  • *=: Multiplication
  • /=: Division
  • %=: Modulus

BONUS: How to Make a Password!

Just in case you wanted something to get you started, here's a quick little tutorial on how to make a password!

  1. In index.php, add this code:
<form action="result.php" method="post" > Password: <input type="password" name="pwd" /> </form>

Now, make a new file named result.php.
Add the following code:

<?php if ($_POST["pwd"] == "your-password-here") { echo "Correct password!"; } else { echo "Wrong!"; } ?>

Lemme back up...
The form is for the form.
Using $_POST we get the information from the form (as said in my PHP Form Tutorial).
And the if statement checks if the password is right.
In the sting that says, "your-password-here", you can change that to whatever you want!
Also, you can change what echoes when you get it right and get it wrong.

Well, I told you everything you will ever need to know about PHP. PHP really is an amazing language-
You can make databases,
and more!
I can't wait to see what you make with this tutorial, and, like I always say, stay six feet or 20 potatoes or 4 squids apart, wear your masks, wash your hands, practice, be curios, and...

Any issues with this tutorial? Any ; I forgot? Please comment so I can update this tutorial. Feel free to post things you make with this tutorial in the comments.
Good websites for practice include:
You are viewing a single comment. View All
Profile icon



I wonder when we will have our next dumb conversation...