Firstly thanks for such a warm welcome, I hope these will live up to your expectations.
Secondly, I want to set out a few prerequisites. I am assuming that you're using PHP as a web server addon (cgi or the like). If you want to use PHP as a command line language ( many *nix people might want to do this) then you can, with a few minor changes which I will not be covering. The easiest method for getting PHP on a web server on windows is through using XAMPP (google it). I won't cover an installation/configuration tutorial unless I get a few people wanting one.
In this lesson I'll be discussing the different data types, and how to use them with variables and output. I'm assuming no knowledge of any programming languages so I'll start at the beginning.
In the most basic form, your websites, scripts and programs will accept data and display information back to the user. Your data might be a username, and your information could be a welcome message. More complex scripts might process the data, such as searching a database or analyzing the data to create graphs etc.
So what is data?
Well, in PHP it usually comes in 4 main forms: Strings, Integers and Floating Points (floats) and Booleans. A String contains a group of characters, and is identified with "". eg. "This is a string". Integers are your basic whole numbers: 1,10, 100, -1000 etc. Your floating point number or "floats" are decimal numbers, 0.56, 95.877 3.14159 etc. And Booleans are one of two values: True and False. A Boolean value is the most basic form of data, and we usually use it to set flags(which I'll explain in a later lesson).
Now, in PHP the basic code structure is:
[statement];
There MUST be a semicolon at the end of a statement or PHP will spit out an error. This is probably the most common error in PHP especially for people new to PHP (I still fall victim to it sometimes).
The last piece of information you need before we try some code is how to display things back to the user. Most people use the "echo" statement. its used in the form of
echo [what];
So - to our first piece of code:
<?php echo "Welcome to PHP, you have coded your first piece of code. Well done!"; ?>
Note the <?php and ?> "tags", these tell the web server that the PHP interpreter needs to be called on the file.
Place that code into a php file (lesson1.php, for example) and place it in a web accessible location on your server. When you navigate to it in your web browser, you should see the output:
Quote
Now, variables
"So what, I can process data but where can I store that data?" you ask. Well, that's where we use variables. Variables are a means of referring to the data; you can store, change and remove data from a variable, create new ones and remove old ones.
In PHP we use the $variableName to identify a variable:
$a = 1; $b = "string"; $c = 3.14159; $d = True;
All of these are storing the value into the variable. If we wanted to do something with $a, we could simply call it as "$a" wherever we wanted:
<?php $name = "Jack"; echo "Welcome $name, This was created using PHP". ?>
Now, next lesson I'll be discussing in more detail bout string concatenation, and using variables in strings etc. but as you can see, its simply and easily replaced $name with "Jack".
Now, there a few rules about variables, some are enforced by PHP (it will spit out an error if you break the rule), and some are what we call "coding style" or good programming practice. Firstly, variables can contain any alphanumeric characters, as well as "_" and "-". You will no doubt come up with your own coding style, however here are a few examples:
$CamelCase $unknownName $thisisveryanoying $USUALLYRESERVEDFORGLOBALS
Now as you can see, the two that are easiest to read are the first two, camel case and (forget-its-name). Variables that are entirely uppercase resemble global variables and arrays ( which well cover later on), and thus, can easily be confused:
$SERVER="apache2.2, php5, mysql5.2"; echo "My server is $_SERVER";
The $_SERVER variable (which is technically an array) is set by PHP/Apache and contains information about the server, however if you wanted to use a variable to store custom information about a server, then you can see the similarities between the two.
When we cover more material such Object Oriented Programming (or OOP for short) you'll start to see exactly how important a good variable naming practice comes to be.
Please, with all my lessons (this one being no exception) give some feedback on how they are going. I do want to know if there isn't enough in each lesson, or if there is too much. If you want more examples, or more explanations then do say because I'm not telepathic
Next lesson (for those that want to know) I plan to look at comments, and more use of strings (and other data types). After we've covered a lot of the basics such as functions, control statements and basic data processing then I plan to come back to coding style because it is ESSENTIAL that you develop one and develop a good one.
For those that want some, I'll come up with a small 'research' or 'homework' activity each week (probably posted the day after the lesson) and it will help you understand a little more about what I've covered.
Lastly (yes, there's a few post-lesson notes), one of the best sources for information on PHP is the PHP manual. you can find it under the documentation link at php.net, and its been a fantastic reference and tutorial for me. Not only is there documentation and basic examples for every function and feature of PHP, but on most pages there is user generated examples which can show you some real life uses of whatever the page covers.
Thanks,
Jack
This post has been edited by Sam Granger: 30 June 2011 - 10:34 AM

Sign In
Register
Help


MultiQuote

