Learn to Code via Tutorials on Repl.it!

← Back to all posts
PHP tutorial part 1 - the very basics!!!
h
Baconman321

Finally!

A PHP tutorial is here. So many people have turned their heads at learning it, but I think it's actually a great language to learn overall. This is designed to help you guys learn PHP in a comfortable way. Right now, I think 3-5 parts may be within what you need to learn for the basics, but I don't fully know. Stay tuned, though. If this gets enough upvotes/comments (not trying to be a cycle hog, just don't wanna waste time making tutorials that no one reads), then I will make a second part! So, without further ado... let's get started, shall we?

What is PHP?

PHP is a general purpose, scripting language, designed especially to suit web development. It was designed by Danish-Canadian computer programmer Rasmus Lerdorf in 1994. It has really grown since then, offering both simplicity and usability.

Extra resources

While my tutorial may cover the basics, it doesn't cover everything. If you guys want to go further, I recommend php.net as a good source.

The first step: Setting up a PHP file

While other programs require you to configure the server, PHP already has it configured for you! All you have to do is create a PHP file (ending in .php), and insert these tags in:

These are called delimiters, PHP executes anything inside those delimiters. Now, the best part about PHP is that it can be embedded in HTML. That's right! You can add HTML to a PHP file (more explained in part 2)! You can also omit the ending ?> PHP tag as long as the file is pure PHP (so no HTML). That's pretty much it, all you do now is put your PHP code between the <?php and ?>! Next up is how to actually code in PHP!

The very basics - variables!

PHP has some weird syntax, so you guys probably should get used to one of the weirdest of the syntax PHP has to offer - variables.
PHP variables are declared like so:

Seems pretty simple, right? Let's break it down.

The $ is called a sigil. It tells PHP that a variable is about to be declared. After the sigil comes the name of the variable. No spaces are allowed. Since PHP variables are dynamically typed, it automatically assumes the value of what you put in. This is different from, say Java or C++ (or C). However, a lot of programming languages (like JavaScript) use this.

The = sign is pretty self-explanatory. It declares to PHP that something is about to be assigned to the variable. In this case, it is the string.

The "hello world" is the value we are passing into the variable. This is what will be stored.

The ; is a semicolon. It tells the computer that the statement is complete. Unlike Python and JavaScript, semicolons are required in PHP.

Ok, that wasn't too hard, was it? Let's go on to the next thing we need to know - output!

Output

Almost every programming language has some way of output. In PHP, the basic output statement works like so:

echo outputs whatever is in front of it, followed by the value. Again, the semicolon tells the compiler that it is done with the statement.

Comments

Comments are either inline, or multiline. Inline comments can be created by two different ways, either by using // or #.

Multiline comments are declared with the /**/ format.

That is all on comments, now let's move to math operators!

Math operators

Math in PHP is similar to plenty other languages. There are the traditional ++, --, +, -, /, %, **, +=, -=, /= and %= in PHP, and we are going to break down what each one does.

The increment operator (++) increases an variable holding a number's value by 1. The decrement operator (--) does the opposite, decreasing a variable that holds a number's value by 1.

The addition operator (+) adds to a number variable whatever is to the right of it. The subtraction operator (-) does the opposite, subtracting to a number variable whatever is to the right of it.

The rest of the operators do what you think they should do.
The multiplication operator (*) multiplies, the division operator (/) divides, the remainder operator (%) gives the remainder of a division equation and the power operator (**) raises a number to the exponent of a number.

The +=, -=, /=, %= and **= operators assign a variable whatever is on the right of the operator. Instead of having to write

, you could instead write it like so:

Same goes for the rest of the operators.

Lastly, we have the concatenation operator. This adds two strings together. In PHP, it's a bit different. Here's an example:

As you can see, the . operator concatenates two strings. You can use the .= operator in the same way you use the += operator, to add to a variable whatever is on the right side. There are many more operators, especially for comparison, but you can read more about those here, because most of them are the same as other languages.
And last but not least for this part 1 tutorial, the If then statements!

If then statements

If then statements are part of the logic in programming, they provide conditional operations, executing certain things based off of a comparison. The if statement will only run if whatever put into it evaluates to true. The else statement will execute if whatever put into the if statement evaluates as false. Additionally, there are also else if statements, allowing you to compare things even farther!
I assume you read up about comparison operators, or at least know what they are from experience on other programming languages
Example using if statements:

As you can see, the syntax is very similar to most programming languages.

Conclusion

Well, this is the end of part 1. Please, comment and upvote if you like. If you don't, please tell me what I can do better? Part 2 to come out soon, if this gets enough recognition. Thanks for spending time to read this. Have fun! Bacon boi out!

Voters
IntellectualGuy
programmeruser
Baconman321
Comments
hotnewtop
JasonLiu19

Uhhh... what happened to multiplication?

Baconman321

@JasonLiu19 Oh, I forgot that XD. I'll add that in. Too bad no one really cares about this, PHP is a really good language overall :(

JasonLiu19

@Baconman321 I want to learn how PHP can be used with HTML and stuff

Baconman321

@JasonLiu19 Ah. That would be part 2. Also, I think I'm gonna add something to part 1 cuz I forgot it.