Skip to content
← Back to Community
PHP | All You Need to Know!
Profile icon
Whacko

ULTIMATE PHP TUTORIAL!

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...
Repl.it only supports PHP in PHP repls. If you create an HTML repl and add a PHP file, this happens:
image
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.

IMPORTANT NOTE: Use PHP Web Server for HTML, not PHP CLI.

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.

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

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

Output:
image
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!"; ?>

Output:
image
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!"; ?>

Output:
image
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]; ?>

Output:
image
And, changing the data from the array:

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

Output:
image

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:
image
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!"; } ?>

Output:
image
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":
image
You're probably thinking:

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

WRONG!
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:

OperatorNameExampleResult
==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

OperatorNameExampleResult
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 (W3schools.com).

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.
Output:
image
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,
forms,
quizzes,
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...
DON'T STOP CODING!

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:
-W3schools.com
-TutorialRepublic.com
-Repl.it
Voters
Profile icon
ChristianWill13
Profile icon
aleksandra1978
Profile icon
DiveshTheReal
Profile icon
Bookie0
Profile icon
Kookiez
Profile icon
lightningrock
Profile icon
EpicGamer007
Profile icon
DynamicSquid
Profile icon
Whacko
Comments
hotnewtop
Profile icon
EpicGamer007

I would love it if you went over how to do the cools tuff, like saving data which is inputed by the user, etc.

Profile icon
Whacko

@EpicGamer007 Eh, this is just a tutorial for basics.

Maybe I can make my next tutorial PHP advanced...
Profile icon
EpicGamer007

@Whacko please do :D

Profile icon
Whacko

Sure! I'll get to work on it later @EpicGamer007.

Profile icon
Bookie0

Pretty precise, great work!

Profile icon
Whacko

Thanks @Bookie0!

Profile icon
Bookie0

np! :) @Whacko

Profile icon
Whacko

longest tutorial I ever made
@Bookie0

Profile icon
Bookie0

lol good job! :D @Whacko

Profile icon
Whacko

thanks @Bookie0
I wonder when we will have our next dumb conversation...

Profile icon
Bookie0

lol this isnt dumb @Whacko

Profile icon
Whacko

@Bookie0 Eh.
We do have pointless conversations sometimes.
Like that one about special characters XD

Profile icon
Bookie0

oh yea lol @Whacko youre the guy lol

Profile icon
Whacko
Profile icon
DynamicSquid

Neat! You don't see a lot of PHP on here :)

Profile icon
Whacko

Yes, and PHP really is an amazing language, so I knew I had to make a tutorial on it. :)
@DynamicSquid

Profile icon
lightningrock

Markdown issue?
image

Profile icon
Whacko

@lightningrock well that bad

Profile icon
Whacko
Profile icon
lightningrock

ahh much better @Whacko