PHP Noob Basics
Muhammad

Muhammad @th3n00bc0d3r

About: By helping others, you learn how to help yourself. When the Noob is at work, remember it's not a Noob at work.

Location:
Lahore, Pakistan
Joined:
Jul 28, 2019

PHP Noob Basics

Publish Date: Aug 12 '19
17 12

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

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

<?php echo 'Hello Noobs';?>
Enter fullscreen mode Exit fullscreen mode

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

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

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

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
?>
Enter fullscreen mode Exit fullscreen mode

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
?>
Enter fullscreen mode Exit fullscreen mode

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
?>
Enter fullscreen mode Exit fullscreen mode
<?php
 $a = 1;
 $a--;
 echo $a; //Output 0
?>
Enter fullscreen mode Exit fullscreen mode

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';
  }
?>
Enter fullscreen mode Exit fullscreen mode

Now the same can be written as follows also;

<?php
  $a = true;
  if ($a) {
    echo 'I am not a Noob';
  } else {
    echo 'Noob Here';
  }
?>
Enter fullscreen mode Exit fullscreen mode

While Loop
It keeps running a piece of code till a condition is not met.

<?php
 $a = 1;
 while ($a <= 3) {
  echo $a;
  $a++;
 }
?>
Enter fullscreen mode Exit fullscreen mode

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;
  }
?>
Enter fullscreen mode Exit fullscreen mode

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];
?>
Enter fullscreen mode Exit fullscreen mode

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.

Functions
A function is a preset piece of code, that you can use multiple times.

<?php
  function sayNoob() {
   echo 'You Noob';
  }

  sayNoob();
  sayNoob();
?>
Enter fullscreen mode Exit fullscreen mode

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

Noob Index

Comments 12 total

  • Muhammad Asyraf Hadi B Mohd Shukri
    Muhammad Asyraf Hadi B Mohd ShukriAug 12, 2019

    hai, my name is asyraf.. would you be my mentor in learning programming

    • Muhammad
      MuhammadAug 12, 2019

      Why Not, ill be more obliged to, just follow up my guides and keep me posted on what i am unable to make you understand and ill try my best to make it as simple as possible under all my capacity.

      • Muhammad Asyraf Hadi B Mohd Shukri
        Muhammad Asyraf Hadi B Mohd ShukriAug 12, 2019

        ohh, thank you.. can you give me a list of fundamental of programming that i need to master first.. and can you explain why there a lot of programming languages?

        • Muhammad
          MuhammadAug 12, 2019

          Please Follow the guide

          Noob Guides

          and well just like people speak in soo many different languages from different places, maybe thats why people have preference in different to how the same thing can be achieved in multiple ways.

          • Muhammad Asyraf Hadi B Mohd Shukri
            Muhammad Asyraf Hadi B Mohd ShukriAug 12, 2019

            I'm curries about the usb that you mention in your guide.. why we must use the usb?

            • Muhammad
              MuhammadAug 12, 2019

              Because it is convenient, rewritable and fast than a typical CD or DVD.

  • Ihor Vorotnov
    Ihor VorotnovAug 15, 2019

    Great post for beginners! Love the simplicity and very clear examples.

    However, I'd recommend teaching best practices from the early beginning, e.g.: if ($a == true) should be if ($a === true). Always prefer strict type comparisons.

    • Muhammad
      MuhammadAug 15, 2019

      Totally Agree to you, its just that, i am wanting to really really build a base, and as we go on if you look at my Noob Index, by the time we complete the NOOB CMS, i should be able to implement all best practices by the help of people like you to help me through.

      • Ihor Vorotnov
        Ihor VorotnovAug 15, 2019

        Kind of makes sense, I guess... But, I never get why teach bad practice first for sake of some "simplicity" and then teach the better one on a later stage. Especially when both bad and good ways of doing stuff are identical - e.g. in this particular case it's == vs ===. These are exactly the same for a newbie, so just pick === from the very beginning with a short note saying why it's better than ==. Building up from simpler to more complex stuff makes sense for teaching broader topics and concepts - OOP, dependency injection, autoloading etc. For basic stuff, imho, it brings more confusion than necessary on later stages.

        • Muhammad
          MuhammadAug 15, 2019

          I have an idea, based on your conclusion, Once we complete the bad version, I will add a section to it, Unit Testing or perhaps Refracting the Code. That is where I with your help and other helpers can really create a basis of how anyone else code or perhaps a piece of code can be tested before made production.

          • Ihor Vorotnov
            Ihor VorotnovAug 15, 2019

            I like the idea :) Not sure 'Unit Testing' fits here (too complicated topic for newbies), but 'Refactoring the Code' sounds logical. A separate post going through all the code and explaining where, how and why it can be improved. I'd be happy to co-author that post with you.

Add comment