When learning anything new, we all have to start somewhere, so lets start at the utter basics. If your an expert you’re going to want to go right a head and navagate somewher else, or be sure to check out the WAMP article I did which might help you if your a php developer on windows.
Before I start, I want to just explain a bit about php, where it comes from. PHP is a general-purpose scripting language, which goes hand in hand with web development, it can be embedded into most languages like HTML. When it comes to web development, it’s used in a combination with MYSQL - also because php is open source, you will often hear the the term ‘LAMP Setup’. Now its not literally a lamp, it means Linux, apache, MySql & PHP. They all work lovely together.
Now someone might dispute me on this one, but I had last heard that PHP was the most popular web language, beating Java (not to be confused with javascript which is a different language). However if I’m wrong, its certainly the most popular apache module.
Now I find this very important. To know about your language and its orgins before learning it, sort of like thanking the person for your tool of your trade. Rasmus Lerdor created php (or FI as it was previously known) way back in 1995. Today we work with PHP5, which has many advancements, but essentially remains the same. Thats all I want to touch on that, but I do urge you to go find out some more on php.net’s website.
Right today, we are going to start with a simple hello world script. This will output on screen, Hello world, every language has one of these, and because PHP is so easy, this is nice and lovely.
For those who are using WAMP, make sure you have it started up.
1. Start, programs, wamp, start that up.
2. Open up your editor of choice, but for this I will use notepad.
3. The code to make this work
<?php echo "Hello World"; ?>
4. Save as hello.php and if your using wamp, save it to c:/wamp/www/testing/hello.php
5. Ensure all the wamp services are started, right click on its icon near your clock.
Type into your browser this:
http://localhost/testing/hello.php -You should see Hello world on screen. If you have any problems you can comment below.
Now its important to go over what is actually happening. PHP is a server side programming language. This means your browser has to load up the page, request it from the server, the server then executes the PHP, and outputs back to the browser. This is different to say a client-side programming language, like flash, or javascript. Though the page is still requested from the server, the code itself is executed by the browser. Meaning the user is required to have either flash installed, or javascript support turned on, where as with php, the user does not, as the script is executed by the server first. This is the definative difference between server-side & client side.
Ok, lets look at the first part of the code, not a lot to anaylse here but we will anyway.
<?php
This is on the first line, this indicates to the server that php has started, and whatever comes after it is going to be php code. Which is great, because we can start and stop php as much as we like, and thats how we can include html within our php, or php within our html.
So we’ve started php going, we next need a command, to output to the text to the browser, and we’re using echo.
echo "Hello World";
Now its important to see whats going on here, we’ve started the command echo, then within the quotation we have the text we want to say. The quotes allow us to output anything within them, numbers or letters, but this will be a string. Don’t worry too much, we’ll cover more of this later, the differences, etc, what a string is.
One final point is that the line is ended with a semi-colon ; as this tells php to end the command being run, this the syntax of php, and will throw up an error if you miss it. There are times when it isn’t required, but as we progress you will understand.
Then finally, we end our php code with:
?>
This tells the server to stop, and we are finished with PHP. I hope this has got you started, and has you enthusiastic about learning PHP and returning for more. Other tutorials I will include a video, however this one I didn’t feel was justified.
You're Thoughts & Discussion
No comments for “Learn PHP Basics - Hello World PHP Example”
Post you're thoughts...