Jump to content
xisto Community
Sign in to follow this  
gavacho

Php In Five Minutes Five minute introduction to PHP

Recommended Posts

A five minute introduction to PHPIf you are like me, you are intimidated by long, drawn-out tutorials that ask you to write a lot of code. I am a purpose driven learner. Any programming learning that I do is what is needed to accomplish a particular task, so I have neither the time, nor the interest in learning all the ins and outs of a programming language. In the case of PHP, trying to learn about all areas of PHP would probably require a lifetime, and it would be futile because the language is constantly changing and evolving. To me, the easiest way to learn a programming language is to just learn the very basics, then get right into using it for practical purposes, thereby learning by doing. This is easy because I have access to the internet, the greatest storehouse of knowledge, and greatest university that ever existed, right at my fingertips.So that said, take a look at your watch. You are going to learn all you need to get started with PHP in five minutes or less!First, lets open the sophisticated (not) software that we need to create our first PHP script, Windows Notepad. I use notepad because it is as simple as it gets, and is on every Windows computer, but any plain text editor will do. DO NOT use applications such as Microsoft Word to do any programming ever, because it adds hidden characters to files.In Notepad, type the line "This is my first PHP script" (with quotation marks), then, and this is critical, then save the file as myscript.php. This is critical. You must save it with the .php extension instead of a .txt extension. You do this by clicking the arrow beside the "save as" drop-down file type list in the Save dialog box, and saving it as "All files".At this point, you have a php file, but no way to view it, since a browser cannot open a php file directly. PHP is a server side scripting language (as opposed to javascript which is a client side scripting language). Therefore you need to have a server with PHP installed in order to view your php file. You can use a remote server with PHP installed to do this, but what I recommend is to test your files locally using a server installed on your own computer. You can install an Apache server on your Windows computer using a package called Wamp (http://www.wampserver.com/en/) which is free, and installs everything you need with one click, or use a similar package.Ok, now that you have a server installed, navigate to your file in the browser with your server running. You will see the text you typed. Now what you have is a PHP file, but it does not contain any php code, so it is not really a script. Let's add some php.First, type <?php on the line above your text in your myscript.php file.Then type ?> on the line below the text. All php code must go between these opening and closing tags. Try browsing to your file again. Now all you get is a blank page. In order to view text that is incorporated within a php script, we must use an echo statement. all we do is place word echo in front of the quoted line of text, and a semicolon afterwords. Every php statement MUST end with a semicolon (:lol:. Now the script looks like this:<?phpecho "This is my first PHP script";?>Now browse to it and you can see the text again. Note that any text strings used in php must be contained within quote marks. Alternatively, you could have placed the text as html code on the page, outside the php tags, like this:<?phpecho "This is my first PHP script";?><p>This is some html on the same page as the php</p>Were done!we have just learned enough to get started with php. This is because the entire php language is comprised of 100's (maybe 1000's) of built in functions that do all of the tasks that you need the language to do. All you have to do is choose the function you want, and enclose it within the php tags like so:<?phpphpinfo();?>This is a particularly useful function, because if you browse to it, it will display detailed information about your php installation.All other functions work the same way. Some require parameters between the parentheses, and others, like phpinfo(); don't.Where do you find all the nifty php functions? Download the php manual as a Windows help file from PHP.org, or leave the online version open in a window. There you will find all of the php functions, all you have to do is copy and paste the ones you need, along with everything there is to know about the php language.In this case the function used is phpinfo()but it could have been any of the functions.You can also write inline code in php such as:<?phpa = 2 + 2;echo "a";?>If you browse to a file with this code, the answer, 4 will be displayed.That's it! Everything you need to get started using PHP, the most powerful and easiest web scripting language.

Share this post


Link to post
Share on other sites

<?phpa = 2 + 2;
echo "a";
?>

The above will simply give an error or it might display "a" but it sure as hell will not display 4.
Since a variable is always started with a dollar-mark, it should be:
<?PHP
$a = 2+2;
echo "$a";
?>

And there are so many other basic skills you need to actually start to do some programming. Like the if-else statements, if anyone is going to do PHP they should at least know those.
Edited by Lightning73 (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.