Hey guys. I’ve been so busy lately with some cool projects that unfortunately I haven’t got time to update the blog so often that I used to. I’ve come across some serious problems with the projects and have resolved many of them (I will share with you guys a finished project soon) using stuff I wrote on the blog. I also had to used some dynamic code like PHP and had to extract some stuff from my old notes.
Thats when an idea come to mind, although I know a bit PHP, I’m not that good at it. To get better at this I thought reading my notes would help me but in the mean time sharing them with out guys (using my own words! – meaning I will refer to php.net for some links, would probably talk not so professionally but hopefully some of this would eventually help someone). I will try to separate them by category so they can be easy to read and last but not least, if you guys find errors in my writing (referring to info, code, etc) DO PLEASE CORRECT ME (using the comments section). I will try to write about basic things so don’t expect any advanced-ish things.P.S: I would gladly help anyone out with problems if my time allows me. P.S 2: I will try to write up soon-ish, at least capturing one section a week.
What exactly is PHP standing for?
PHP means Personal Home Page/Forms Interpreter, an dumb name actually which at some point was changed to PHP Hypertext Processor (sounds better right?). The programming language was made by two guys, and has been inspired from numerous languages like C or Perl and other nice things (irrelevant). So basically, to start coding in PHP you need to know things like: PHP itself (data types, operators, instructions or functions, etc), HTML (any PHP code will actually transform into HTML code after processing so its good to know this), working with databases (if you need to store or get data).
You can use PHP mostly for two things. You can use to generate dynamic pages or you can use it to build scripts for server administration (you can use PHP to target different commands to do different things, actually a lot of them). Basically, any webpage on the internet uses HTML (currently HTML5, which by the way its awesome) and there are couple a dynamic programming languages like PHP that are used to provide information from a stored database. Keep in mind that any PHP code will eventually return HTML code and not PHP (HTML is not a programming language and it does not have instructions, functions or whatever). Ok, forget this boring stuff. You need to learn the way I did.
You need to know that PHP files have a .php extension and without it they would not work. No, renaming a .html file into a .php does not mean you have actually PHP code on your server. To be able to make run PHP code on your machine (Windows/Mac/Linux) you need to install a local web server. Bellow are ways how to install on your Machine.
Install a web server on Windows
This one is actually pretty easy. Most of the time I used XAMPP which comes with MySQL, PHP and Perl. You just need to download from the page and using the basic “next, yes, yes, yes” you can automatically have a server installed on your local Windows machine. Pretty easy right? Good. BTW, try to install in on a different partition from the one with Windows on it so if Windows stops working and you need to install it again, you wont loose your PHP code.
Install a web server on Linux
There are some commands you need to enter in your terminal to do this and following THIS tutorial will do the job.
Install a web server on Mac
I currently use Mac and I’m using MAMP. There is a free version but also a PRO version. Just install the dmg file, start the server (using the start button) and we are good to go.
Going further, after installing your server locally, there will be a custom folder where you need to put your PHP files so they can be interpreted by the server. In Windows the folder will be somewhere in “xamp/htdocs” (or something), on Linux they are usually stored in “/var/www” (open a terminal and use “cd /var/www”) and on Mac it depends where you want to make your default folder.
Ok, done that? great. Lets start by showing off with a basic PHP file. There are a couple of ways how to write PHP code, you can use as following:
<?php PHP CODE ?> <script language="php"> PHP CODE </script> <? PHP CODE ?> <% PHP CODE %>
My favorite version is the “<?php ?>” thing because most of the server support it. You can also use the “<? ?>” bit but I saw that some servers don’t support it and you have to enable it. Ok, lets write our first basic PHP file and will wrap up the introduction. So go into your root (“htdocs”, “/var/www” or custom for Mac), create a new file that has a .php extension (right click on Windows [if Windows does not show the extension, you need to enable it from folder options, check this for info], “nano index.php” from the terminal for both Mac and Linux [this will create a new file where you can write your code.]). If you need further info for these, write it up in the comments and I will write up here with answers.
<?php echo "
Hello Bogdan, this is my first PHP script!"; // will output Hello Bogdan, .... echo "My IP is: " .$_SERVER['REMOTE_ADDR']; // we will learn this soon. ?>
Keep in mind that you can use HTML code inside PHP code as we did above. Thats it for today guys, hope you learn some basic things. Let me know if you have questions and hopefully I can explain more. See you soon!