So you are ready to learn PHP? First of all I want to let you know that if you are good with mathematics you will certainly be good with coding PHP or other languages, now the rest is a matter of patience or not. Though don't think of it wrong, some people who aren't so good with maths can code PHP good, it comes with experience. The younger you learn it, the best you'll know it, so do not start to learn it too late!
So here is what you will learn with the lesson of today:
Table of contents
1. Variables and execution
1.1 Types of variables
1.2 Assigning a variable and the function of ".="
2. PHP opening tags and basic commands
2.1 Echo
2.1.1 When to use " "
2.2 Die or exit
2.3 Include
3. PHP Comment tags
4. The difference between ' ' and " " and when it's best to use which
1. Variables
Without variables you can't code PHP, so this is a very important but easy part of PHP.
When is a variable valid? What composition does it have?
- It starts with a $ sign
- It does not have a number or invalid sign after the $ sign
- It does not contain spaces or any other symbols other than an underscore and letters or numbers (but symbols like ä are now accepted because of the ASCII 228 - but that's not so important so we're not going to talk about it)
PHP Code:
//Here are some variables - it's upto you trying to find out whether they are valid or not #1. $var #2. $_var #3. $4var #4. $var4 #5. $var_5 #6 $vär_one_1_2 #7 $var one #8 $9var_two_test_three_4_vär #9 $VARIABLE1 #10 $the_latest___variable
PHP Code:
//Answers #1. $var // valid - nothing special about this variable #2. $_var //valid - may start with an underscore #3. $4var //invalid - may not start with a number after $ sign #4. $var4 // valid - may have number anywhere but not in the beginning #5. $var_5 //valid #6 $vär_one_1_2 //valid ä is accepted in the newer types of ascii code #7 $var one //invalid - contains a space #8 $9var_two_test_three_4_vär //invalid - begins with number #9 $VARIABLE1 //valid, upper or lowercase is not important #10 $the_latest___variable //valid
Execution: after every command in PHP you must put a ; (for example $var = 1; ) If you don't, PHP will be so friendly to show an error and even tell you on which line the error is! It will say something like Parse_error on line [x]
1.1 Types of variables
In total there are 8 primitive types of variables in PHP.
Here they are:
1. Boolean
A boolean can only be 2 things: true or false. Which can also be represented by 1 or 0 -- where 1 is true and 0 is false.
To add the value to this type of variable do as show:
2. Integer
Integer are round numbers like 1, 2, 3, 4 or -5 or -555
PHP Code:
$integer = 100; $integer_two = -304;
3. Float
A float is a floating-point number or a double for example:
PHP Code:
$float_one = 0.942828; $float_two = 23.0;
4. String
A string is, to say it simply, a line of text.
Here you have to be careful when you have to escape a string or not.
First: let's try to assign "Hello world" to the string variable
PHP Code:
$string_hello = 'Hello world';
You can notice there are 2 brackets ' and ' between them you have to put the text. This is so that PHP knows where it starts and stops. Now you can probably imagine that sometimes in your text you need to put a ' like in 'let's' and then PHP is confused! Then you have 2 solutions: use double brackets or escape the string:
PHP Code:
$string_db = "let's go!"; //The use of double brackets $string_escape = 'let\'s go!'; /* just put a \ in front of the bracket this is really important because sometimes you have a text in which you use a ' and a ". PHP will give you a parse_error if anything goes wrong and again, will be as kind to give you the line number :).*/
5. Array
Arrays can contain more than one value. We're not going to explain it thoroughly in this lesson yet because it might already be too complicated. Notice: echo will output the outcome of the variable.
PHP Code:
$array = array('one' => "test", 'fun' => 'cool'); echo $array['one']; //outputs: test echo $array['fun']; //outputs: cool echo $array[2]; //outputs the second one in the array: cool //other use $string = 'Have fun!'; echo $string[3]; //outputs 3d letter or symbol of string: here it would say: v
But there are more ways to use arrays than these ways.
6. Objects
We're not going to explain this so much because it already quite advanced, you'll see it in a later lesson.
php.net Wrote:PHP treats objects in the same way as references or handles, meaning that each variable contains an object reference rather than a copy of the entire object.
7. Resource
For example the return of a function like mysql_connect(); (which connects to a database -- but it's what it returns that is a resource)
8. NULL
The best explanation for this is found on PHP.Net too,
php.net Wrote:A variable is considered to be null if:
- it has been assigned the constant NULL.
- it has not been set to any value yet.
- it has been unset().
Unset is a function, it does that the variable is like it has never been assigned a value.
1.2 Assigning a variable and the function of ".="
So in the previous functions we've always added a php example where the variable is set.
You can see we always use the = symbol to set the value. Now there are several other symbols like the = which set the value but in a different way, one important one is the .= which is realy useful.
Here is a practical example, you will understand it easily.
PHP Code:
$string = 'This'; $string .= ' is'; $string .= ' what using a '; $string .= '.= is like :)'; echo $string; //outputs: This is what using a .= is like :)
This will be used a lot, maybe you can't imagine it yet, but once you will start coding you will see it is really useful.
2. PHP opening tags and basic commands
To start a php file and start coding in PHP you first have to use the PHP tags. <?php //php code ?> those are the tags. <?php = open -- ?> = close
NEVER use <? to open php tags. The chance you'll change host is big and maybe you even want other people to use your script -- some hosts have the small php tags option disabled in their config and if you use <? on some hosts to open the php tags you will get an error.
So here are the basic commands:
2.1 Echo
Echo ''; or echo $var; or echo ""; or anything like that, is just to output the variable. You have already seen examples throughout this lesson.
2.2 Die or exit
Using the die($string); or exit($string); function will stop executing the rest of the page after the command and show $string. This can be useful for debugging to see where an error is.
Example:
PHP Code:
die("The rest of the page is not available for your user group");
2.3 Include
This function includes another page. This can be useful if you have a theming function on your site
include('path_to_file');
PHP Code:
//For example take $current_theme from the database include("./themes/$current_theme/header.php"); //content here
Notice that i use double quotes here. Double quotes parse variables. If you use a single quote and still want to parse a variable, do this:
PHP Code:
//without double quotes: include('./themes/' . $current_theme . '/footer.php'); //so you get out of the quote and put a dot and the variable then dot and then put a quote again if you need to put another part of a string after it.
IMPORTANT: It is important to use simple quotes (' ') when you are not going to put a variable inside it, it decreases CPU usage a small bit but if you use a lot of double quotes (" ") instead when there are no variable you have a loss. It is important to think of the CPU usage and bandwidth and mysql & etc when coding so that the script you are coding is fast.
3. PHP Comment tags
You have several types of comments, here are the most known ones:
PHP Code:
# Comment here //comment here /* multi line comment */
When creating PHP pages, you can use the same program used to create HTML pages. All plain text editor will do. You will also need FTP to transfer files from a computer web hosting. If you already have an HTML site probably already use FTP progra
Our php course says almost the same thing, no need to say this, your post is just like spam, considering there's spam on your signature too, I have warned you.
I work in Php coding . I do Project In Php .First You learn Insert Update Delete Operation From Database In php code using Then After Your Work in Any Project and You want to learn Php You purchage Book PHP with MySql (Bible) and another way to Search On the Internet Write Using about Php and Morethan Site Available Site To learn Php and Cilk On any Site Give Php information or Php tutorial on the site and read all information of Php[/u]
If You want to Use a any Variable No Need to define in the Advance You can Define it When And Where You want to Use it.it's So flexible & lossy Type of Language.
I do PHP coding. I do PHP Projects. First Learn the syntax of the PHP language. Then learn the Add, Update and Delete operation from the Database. You can purchage Book PHP MySql and another way to search on the Internet. Then from the PHP tutorial sites read all the information about PHP.