Get Started in PHP

by Pettrocity in Circuits > Websites

5566 Views, 22 Favorites, 0 Comments

Get Started in PHP

001.JPG
PHP, or Personal Home Page HyperText Preprocessor, is a server end programming language for creating dynamic webpages. PHP originally stood for Personal Home Page, but now the first 'P' stands for PHP. Confusing?? Probably, but definitely helpful. It allows you to connect to a database, store data, print messages, and display variables. This is extremely helpful for creating a website. Almost every website that has a user login feature uses PHP. Also, one of the great things about PHP is that no one can steal your code! 

Example: If you right-click on a webpage, a drop-down menu appears. If you click on 'View Source', you will see a webpage with all the HTML, CSS, and JavaScript on it. However, if you include PHP in that webpage, it won't show the code, only the result of the code.  If you tell the code to display the words 'Hello World' using PHP on a webpage, and you viewed the source, where the PHP was, you would just see the words 'Hello World', not the code. This is because PHP is a Server End code, meaning that it is compiled and executed on the web server, and the result is given to the webpage. Neat, huh?

Terminology:
~PHP: Personal-Home-Page HyperText Preprocessor
~Server End code: Code that is executed on the server and gives the result to the webpage
~Dynamic Webpages: Pages that interact with the user (a login form)

What You Need

001.JPG
The one downside to learning a server end language is that you need: a server. However, this is easily fixed! You can download a testing sever for free onto your computer. All you need to do is search for 'XAMPP', or goto www.apachefriends.org/en/xampp.html , and download the installer for the current version for your operating system. One of the cool things about XAMPP is that you can download it for Windows, Linux, Mac, or Solaris, so you don't need to worry about you operating system you have.

The other thing that you need is a PHP/HTML editor. If you are running Windows (Like I am), I would suggest Notepad ++. It's a free HTML editor that has many file extensions, including PHP, so you can edit the files you create easily. It also will highlight and color the code, depending on what file extensions you use, so you can detect mistakes.If you are running Mac OS X or Linux, Komodo Edit is good. I'm not sure what editors there are for Solaris, so if you do know, please leave me a comment.

For Windows users, Notepad works fine, although I don't like to program in it, because of the lack of syntax hi-lighting. 

Install XAMPP

001.JPG
Once you are on the apache friends website, scroll down and you should see four links and their descriptions:

XAMPP for Linux

XAMPP for Windows

XAMPP for Mac OS X

XAMPP for Solaris

Depending on your operating system, click on the appropriate link. That should take you to a page that has the download files. Click on the Installer for Windows , the XAMPP Linux 1.7.4 for Linux , the XAMPP Mac OS X 1.7.3 for Mac , and the XAMPP Solaris 1.7.4 for Solaris .

On the download page, there should be instructions on how to download, install, and run XAMPP.

IMPORTANT: Be sure to download Apache and MySQL when it prompts you to!

Echo

001.JPG
Once you have successfully installed XAMPP, there should be a desktop icon and/or a start menu icon (for windows, depending on your preferences). Click it, and the XAMPP control panel should start up. You should have Apache and MySQL running (they will be highlighted in green). If not, click them, and they should start. Now you are ready to start coding. Start by creating a file in you text/HTML editor. Save it as echo.php in the htdocs folder, inside the xampp folder in the drive where XAMPP is downloaded - something like C:\xampp\xampp\htdocs . The *.php file extension is just a html page that has php coding in it. The server that you just installed will look for and run the PHP script and then give it to the web browser as an html document, with the PHP output result in it. Write the following into echo.php:

<?php

echo "Hello Instructables community!";

?>

This code, with the echo statement, will simply display the words 'Hello Instructables community!' on an otherwise empty webpage. But before you can run the code, you must make sure that it is saved in the right location. To run it, go to your web browser, and type in this: http://localhost/xampp/echo.php into the URL search box. You should see the result of your code: the words 'Hello Instructables community' in the web browser. If not, check to make sure that all you code is correct, and that it is saved in the right place.

Using Variables

The next step in creating PHP code is variables. You can use variables to store long strings of text, store number, and make writing things a lot easier. Example:

<?php

$str = "RandomText jdsfbjsfgbfsnvfkjjjvm,nbvjbfbmhgggggggggggggvffgxc;"

echo $str;

?>


Now the webpage reads Random Text etc. This isn't very helpful in this situation, but say you needed to repeat the variable over and over. It would be a lot easier to write $str that the actual text.

Variables can store numbers, as mentioned above. This means that they can do math. Example:

<?php

$num = 5.4;
$numb = 1.6;
echo $num + $numb;

?>


The webpage now says 7. Another useful thing with variables is that to add multiple variables together (the text way, not the math), you can use a period between the variables. Instead of saying

echo $num + $numb;

you would say

echo $num.$numb;

This would display

5.41.6

Instead of 7. This works with text to.

Information Files

Here is the file that will correctly display the words Hello Instructables community!. Don't worry, there will be more to come in the way of PHP tutorials,so you can learn more. It is saved as an html file, so just rename it echo.php.


ENJOY!

Downloads