PHP Noob Basics
Tags: php, opensource, beginners, tutorial
Published: 2019-08-12 03:20:54

Now fire up your webserver and create a php_start directory in the www folder.

![](https://thepracticaldev.s3.amazonaws.com/i/dn0hoyboyc7ewulpumcc.png)

Now create a file by the name of php_basic_01.php in it and add the following code.

```
<?php echo 'Hello Noobs';?>
```

![](https://thepracticaldev.s3.amazonaws.com/i/lt7tt1ejfu7w1a1wldmq.png)

Now open http://localhost/php_start/php_basic_01.php

![](https://thepracticaldev.s3.amazonaws.com/i/54vkj6k0qa16spda5hnj.png)

Now right click on the white area and click on view page source.

![](https://thepracticaldev.s3.amazonaws.com/i/8e7xbqqh45ug5lzjqmkd.png)

We see only text and nothing else, all tags and code has been stripped off, or in terms rendered into plain text.

![](https://thepracticaldev.s3.amazonaws.com/i/by6a66a8dbplg5d3827k.png)

## What is PHP?
PHP stands for personal home page which is now known as Hypertext Preprocessor. Now in PHP, the code runs on the server and ouput is shown on the viewers computer. In our case right above, our computer was both the server and the viewer because in the viewer case we didnt see no code in view page source.

*<?php* this is how we start a PHP code block 
"?>" this is how we end a PHP code block

```
<?php
// All your code goes in here
?>
```

## PHP Quick Gothrough

**Variables**
A variable is a holder for a value, in PHP it starts with a $(dollar) sign.
```
<?php
  $word = 'You Noobs'; // This is a String Variable
  $a = 1; // This is an Integer Variable
  $a = 1.0; // This is a Float Variable
  $a = true; // This is a Boolean Variable
?>
```

**Increment and Decrement**
You can automatically add 1 and subtract 1 from a number using the following code

```
<?php
 $a = 1;
 $a++;
 echo $a; //Output 2
?>
```

```
<?php
 $a = 1;
 $a--;
 echo $a; //Output 0
?>
```

**If.. Else Loop**
It is simply a piece of code to put conditions based on comparing values.
```
<?php
  $a = true;
  if ($a == true) {
    echo 'I am not a Noob';
  } else {
    echo 'Noob Here';
  }
?>
``` 
Now the same can be written as follows also;
```
<?php
  $a = true;
  if ($a) {
    echo 'I am not a Noob';
  } else {
    echo 'Noob Here';
  }
?>
```

**While Loop**
It keeps running a piece of code till a condition is not met.
```
<?php
 $a = 1;
 while ($a <= 3) {
  echo $a;
  $a++;
 }
?>
```
The code in between while, will run 3 times until $a becomes 3 and it will stop.

**For Loop**
This loop runs a piece of code for a set number of times, till the condition is met.
```
<?php
  for ($a = 0; $a <=3; $a++) {
   echo $a;
  }
?>
```
This will produce the same output as the while loop but it is very handy when dealing with arrays.

**Arrays**
An array is a variable that can store more than one values;

```
<?php
  $numbers = array(1,2,3);
  echo $numbers[0];
  echo $numbers[1];
  echo $numbers[2];
?>
```
Now here the [0] is the index of the array and it always starts from 0. As you can see in the diagram, this is a graphical representation of an array.

![](https://thepracticaldev.s3.amazonaws.com/i/jrywnvo4j9svb7rujxm1.jpg)


**Functions**
A function is a preset piece of code, that you can use multiple times.
```
<?php
  function sayNoob() {
   echo 'You Noob';
  }

  sayNoob();
  sayNoob();
?>
```

Now this cheatsheet is really the basics of the syntax on which we will build our application.

[Noob Index](https://dev.to/th3n00bc0d3r/noob-guides-index-4mne)