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';?>
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
?>
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.
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.
Top comments (12)
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 beif ($a === true)
. Always prefer strict type comparisons.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.
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.
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.
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.
I have added you on the Noob Index
hai, my name is asyraf.. would you be my mentor in learning programming
Why Not, i
ll be more obliged to, just follow up my guides and keep me posted on what i am unable to make you understand and i
ll try my best to make it as simple as possible under all my capacity.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?
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.
I'm curries about the usb that you mention in your guide.. why we must use the usb?
Because it is convenient, rewritable and fast than a typical CD or DVD.