PHP Basics - PHP Beginner Tips
12,073 ViewsPHP Tutorials March 19th, 2007
Alright, just thought that instead of leaving you guys in the dust with no new tutorials, I could write a small, simple (but good) tutorial on some of the basics of PHP. If you look through all the tutorials on this site as a beginner to PHP you may not understand a lot of it, and need a back to basics explanation of everything, so with that in mind this tutorial will cover -
1. Conditions (if statements)
2. Loops
3. Basic Functions
Beginner to intermediates can benefit from this tutorial, you may just pick up a tip or 2!
Alright, before we start learning anything from the actual list above, I think it would be beneficial to learn how PHP actually works. You can't learn the basics of PHP without knowing the base of PHP first.
PHP is a server side scripting language, this means that it is executed on the server, and the results are sent back to the browser as HTML.
When testing out your PHP applications, you can't just create the file, save it and open it locally on your own computer, you either need to install Apache, PHP and MySQL (if your using a database, which you should). All that is pretty complicated, so an easier solution is to get webhosting. You can find webhosting for as low as $1 (which isn't exactly the best, but ok for testing). Once you get webhosting, you can just use an FTP program (easy to use) and upload your files onto your website, most webhosts, actually I would bet 97% of webhosts have PHP installed, so it should be easy to find one. Also, just a side note when you create PHP applications if your file has PHP in it, you need to save it with the .php extension so the browser knows to execute it as PHP.
Now, probably the most basic (and most over-done) PHP example is
-
<?php
-
echo 'Hello World!';
-
?>
Alright, if your completely new to PHP this may need some explaining. The <?php part of the script, tells your file that the PHP is starting. Now you may be asking, why do that if you can just save it as a .php, well that is because PHP and HTML work very well together, you can put PHP into HTML just about anywhere as long as you just open with the opening PHP tags and close with the closing tags (?>).
Next we use an echo, this is how we output text to the browser, echo 'Text Here'; is the proper format, notice the ending ; on the line, most lines of PHP require you to input that, there are exceptions that we will cover later though.
PHP Conditionals (if statements)
Probably in every PHP script, you will use conditionals, from if statements to switch statements they are everywhere, let's get right into it and start you off with a simple if statement, probably the most used conditional.
-
<?php
-
if (1 == 1) {
-
echo '1 really does equal 1!';
-
}
-
?>
Alright, this is probably the easiest if statement you will ever see, it doesn't even have an else part to it. If you can't understand this (which I hope you can) then I will explain it, all it's doing is saying if 1 is equal (using 2 equal signs is how you define equal to) to 1 then continue, and all we do is echo out a simple statement. You can do anything though, you don't have to echo. Then we end the statement. Now let's use the same thing but add on an else, so if it doesn't equal one we have somewhere to go.
As you can see, all we did was add on } else { then add another echo statement. Pretty straight forward, but let's say we are using variables, we can make another tiny bit more complicated example.
We use variables in this example, we have a number variable ($number) and an answer variable ($answer), we define static data to these variables, meaning they don't change, but that will be different when you start getting into MySQL and such.
Then instead of comparing 2 numbers in our if statement, we compare our 2 variables. Pretty much the exact same thing, except now our variables both contain different numbers so our output will then be Aww, 1 doesn't equal 1. because our 2 variables did not equal each other, so we go to our else statement.
This is just the basics of if statements, but this is also just a basic PHP tutorial. If you want to learn more about if statements check out any of the tutorials on this site, chances are they use if statements in them.
Now let's check out switch conditionals, these are in theory the same as if statements except they allow you to have a lot of different answers to them. Here, I will show you a simple example first -
At first glance, this may look a bit overwhelming, but break it into parts in your mind. Let's assume that we have a form where a person chooses a favorite animal, and then there chose is saved into a variable called $var. From there we output an answer depending on what they choose. First we define that we are starting a switch, we want to then put in what we want to check, then we have each case, a case is what we are trying to match with our switch, if our switch and one of our cases match we continue on with that case, if not we keep checking through them. If our switch matches none of our cases, then we have a default, from there we just put out any old answer that you want your user to see.
PHP Loops
Loops are great for executing sections of code over and over again to do the same thing, I know when I first started in PHP I though, "what's the point of that?" well when you get more and more advanced it will become a lot more useful. We will discuss, for loops and while loops. First, we will start with for loops, let's make a for loop that outputs the numbers 1 to 10, each number on a new line.
-
<?php
-
for ($n = 1; $n <= 10; $n++) {
-
}
-
?>
For being such a small piece of code, this can be actually quite confusing, but if you understand it, you will think "Doh, that was easy". Ok, first you use the for loop function, now this is where you do all your variable defining, the first part is where you define the value of your first variable, then from there you make your condition saying, if that number is smaller then or equal to 10, continue, and then if it is smaller then or equal to 10 we add one onto our variable ($n++ means add one to that variable). Then we execute a piece of code, now the next time we go back, but our $n variable equals 2, and it does this until $n is equal to 10.
Now for the while loop, this one is a bit easier to understand, and is used more often (well I use it more often) it is used VERY often when dealing with MySQL, people use it to extract data from there MySQL results, we won't be covering that in this tutorial though, for my example I will just be showing you an example of how to do the same thing we did with our for loop
-
<?php
-
$n = 1;
-
-
while ($n <= 10) {
-
$n++;
-
}
-
?>
This will do the same thing as our for loop, except with more code and not as compact, usually a while loop will be used more when working with databases. As you can see, we define our $n variable outside of our loop, then we just do a easy to understand loop, and at the end of our code we add 1 onto our $n variable.
Basic PHP Functions
In the last section of our tutorial we will cover some basic functions in PHP that every user should know, and most likely will use.
These functions will deal with strings, first we will start with a function that will count all the characters in a string (this includes spaces).
In this example, we define a variable, in this variable we define the string we want to use, next we use our function, we assign the output of this function for better coding practices, we use the strlen() function, all it does is output a number based on how many characters are in the string, then we echo out our variable, that will give us our answer, in this example our answer is 12.
Next, we will use a function that turns our string into an array, this has many usages, in this example we will use the same string that we just used before.
Ok, again we make a variable, and define our string in our variable, then we use the explode() function and assign it to a different variable, now the explode function takes 2 arguments, a separator and a string. We say that our separator is a space, (thats the blank space) and that our string, is the variable that we defined before. Now after doing that, we need to echo out what part of the string that we want. When you use the explode function, it turns our string into an array. When you make an array, the first item in the array is defined as [0], the second item is [1] and so on, so if we wanted to access the word City, which is the 3rd item we would want to echo out $array[2].
That is all for now, if you have something you want to learn more about in PHP, just leave a comment and I will add it onto this tutorial!
Thanks for visiting TutorialCode.com
Sean

(13 votes, average: 4.38 out of 5)










April 4th, 2007 at 6:42 am
Hi
I m very beginner to PHP and I got very useful information and guidance from your side . After using your tutorial I can’t stop my self to say thanks 2 you.
Thank u again
Rajendra
April 4th, 2007 at 5:24 pm
Hey, this site is incredibly informative. The basics are still sinking into my under-developed brain, haha. I have a feeling I'll be using your site for a referance in the near future.
Take care,
Chris
April 4th, 2007 at 8:03 pm
I'm happy to hear that! I'm glad you have learned something from my site. Glad to hear you will be making return visits as well! If you ever need extra help don't be shy to send me an e-mail through the contact form on this site.
April 8th, 2007 at 1:54 am
The basics were really helpful. Thank you. Can you please tell me how to make programming more interesting and fun.
April 8th, 2007 at 9:19 am
Programming can be very interesting and fun, making something from nothing is very hard to do.
The beginning can be a bit slow, but once you really start to get into it and learning some more advanced techniques it is quite fun to program websites using PHP.
Just keep on trucking along, don't give up just yet in the beginning. Stick with it for a while longer and you will see that it is actually quite fun and interesting. Learning new techniques how to do something, and learning totally new things in PHP.
April 10th, 2007 at 6:22 am
your tutorial has been a lifesaver.thanks heaps
April 11th, 2007 at 4:33 pm
Today is the day I've decided to learn PHP (because I need it for a pretty big project in Flash). But Flash alone isn't good enough so I needed to learn PHP. This tutorial helped me a lot and I hope I'll find other useful tuts on this site that will help me in the future.
Thanks for creating this website and good luck in the future! And if you accept a little suggestion: some video tutorials would be also welcomed.
April 11th, 2007 at 6:11 pm
Thanks for your kind words, I am glad you learned a lot from this tutorial, that is what this site is for! Is too inform people!
Video tutorials is a good way, but I am not sure how well something like that would work with coding.
The great thing about coding tutorials is that you can just copy paste the code and ta-da, you have it all done! With video's they would have to write out all the code themselves.
I could always provide a link to the tutorials source file though. We will see what the future holds for TutorialCode!
April 11th, 2007 at 10:21 pm
[...] Basics 2 - A Little More Advanced Techniques April 11th, 2007 1 Views Alright, so my last PHP basics tutorial seemed to help quite a few people, and that tutorial only tipped on the edge of the iceberg. I [...]
April 13th, 2007 at 3:42 pm
a verry good tutorial indeed , don't stop , keep up the good work , i'll be lookin forward for new tutorials from u , i got u on my favorites list
April 14th, 2007 at 2:57 pm
I'm a teapot in the PHP (e.g. I don't know it at all) but in this tutorial I understand everything.
Thanks
April 16th, 2007 at 7:27 pm
Hey I'm redoing my website from a photoshop/flash site to a php site & thought I'd stop by & thank you for your tutorials as I'm sure I'll be reading them all to try & get a better understanding in php coding.
April 16th, 2007 at 9:10 pm
I'm glad that my tutorials can be of help to you!
If you ever need help on tutorials, just send me an e-mail through the contact form on this site or even just leave a comment on any tutorial I will usually (hopefully) contact you back a.s.a.p!
May 2nd, 2007 at 1:52 am
Thanks. Very nice article.
May 9th, 2007 at 1:46 am
thx! very helpful indeed!
May 24th, 2007 at 10:26 pm
Hey I would like to thank-you for this tut!
I am new to PHP but have been doing web designing for a while now. I have friends that live off of PHP (lol) anyway like it says at the top of the tut: "I would bet 97% of web hosts have PHP installed" my current web host is in the terrible 3% that does not have PHP, hopefully they will allow me to use it because I have been with them for a while now. If anyone knows of any good cheap (but great) hosting services please post back w/link ty!
-Beefteck
May 26th, 2007 at 3:38 pm
This tutorial is very helpful and very nicely written so that every beginner can understand it and understand every single step...............like me.........lol
Well, i am very thankful to you and to this site which provide us such a nice tutorial.
thx
June 26th, 2007 at 6:51 am
hai boss
this site is very useful for me as a beginner.I can catch all points promptly
so thanks very much.................
July 20th, 2007 at 3:03 pm
Yo thx , Your tutorials are best on the net that i found , very usefull easy to understand thx
July 24th, 2007 at 4:15 pm
what a great guide! I have been trying to learn php, and this sure would have been a help when I was first looking into it. Way to help people who are just beginning to get a grasp of the information!
Thanks,
pemdas21
August 17th, 2007 at 1:40 am
thank you. plz sir send me online comments code with procedure.
thank you
October 27th, 2007 at 10:13 pm
[...] Click here to read more. [?] Share This Bookmark It HereSubscribeBlinklistBloglinesBlogmarksDiggdel.icio.usFacebookFurlMa.gnoliaNewsVineRedditStumbleUponTechnoratiBookmarks: Posted in PHP | Leave a Comment [...]
January 24th, 2008 at 12:45 pm
Hi
I really appreciate you taking the time to help us "wanna bes" One question I do have is what editor (besides notepad) is there that I can use to pratice writing php with? I can't use my server because it's a live site and I haven't figured out how to set my server here at home up,(maybe before I die)so I want to practice what I am learning here because I learn by doing more than I do by just reading it.
Thanks again and have a great day
March 15th, 2008 at 6:33 pm
ok i'm trying out all these damn tutorials cuz i made a friend of mine a template for his private game server website and now i need to code it.
so i guess my first question should be: where the hell do those PHP box thinggies come from?
what program do you use to "type" it?
i got style sheet CSS but that doesn't look anything like any of this crap...
someone help an idiot out, please..
June 9th, 2008 at 4:07 am
Some nice tips, for the beginner. And a nice reminder for those who have worked with php alot before.
June 14th, 2008 at 2:16 am
I already had basics of Java. That and your infirmative site made my job of learnin php as easy as takin a raddish out in monsoon thnx