PHP 101 by Geramy Baxton and revised by Taff Nouvelle,
Okay so lets start...
First:-
<?
This symbol is used To start a php script. We put this at the beginning of our PHP file to tell the browser that what follows is php.
Close the script like this....
<?php
if and echo.
Example "if" Statement Below.
__________________________
<?php
if ("hi" == "hi")
{
echo "heeeeyyyyy";
}
?>
__________________________
This will compare the values of the two parts withing the brackets, and if they are the same, will send heeeeyyyyy to the screen.
VARIABLES.
Naming Rules for Variables
* A variable name must start with a letter or an underscore "_"
* A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ )
* A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($myString)
$String2 = "Hello";
$name = "Hello";
is correct.
$2String = "Hello";
is WRONG.
The names of the variables do not make them a particular type of varible.
The value is set after the equal sign.
This is a string... $String = "hello";
This is an integer... $Integer = 5;
This is a float... $Float = 1.0;
and This is a boolean... $Bool = true;
Converting from one variable type to another is similar to LSL, so;
(integer)$String;
will take the value of the number in the string and convert it to an integer.
The value in the string MUST be a whole number in this case, so;
$String2 = "101";
(integer)$String;
will return an integer value of 101
$String2 = "1.01";
(float)$String;
will return an float value of 1.01
$String2 = "Hello";
(integer)$String;
will return an error.
WORKING WITH VARIABLES.
__________________________
To add two string variables together use the period symbol between the variables,
'Hi there ' . 'Hello man';
this will give a resulting string of
'Hi there Hello man';
To add the values of two numbers, integers or floats use
$Var += $SecondVar;
$Var =5;
$SecondVar =6;
$Var += $SecondVar;
will return 11.
__________________________
$_POST and $_GET
$_POST is used to send data from an html form to a PHP script.
__________________________
<?php
if ($_POST[''])
{
}
?>
__________________________
This will not display on the screen, it will only send the data to the script.
If you want people to see your info in the url use $_GET and that is also used in hyper links for html.
Next is explaining "www.hi.com/?check=hi+there" the question mark starts it, After that we have "check" thats the variable, The equal sign is what points to the stuff after it into the varible, We also call variables "objects".
Example of echo with html
<?php
echo "<h3>heeeeyyyyy</h3>";
?>
Any questions. end of part one.
Here we go lets start ladys and gentlemen.
Every if statment or function you make starts with a opening bracket after it and has a closing bracket after the opening.
Whatever you want to happen goes after the opening bracket.
A if statement starts like this if (
so its if and a opening curly to the statement we want to make
if ($varbile == $Var)
now your statement goes after the opening curly so what we do is put our varible there and a operater then see if it is equal to our second varible. we will go through all the operators next class so lets continue.
Example of if statement with brackets.
<?php
if ($varbile == $Var)
{
echo "$varbile is equal to $Var";
}
?>
That is how a if statement looks and works
hmm what is left ah i know now a else statement but not much of a statement so if ($varible == $Var)
then we have else after it else means if its not equal to it or if its different from one of the if statements you called.
you can call more then one if statment by going like this see example below.
<?php
if ($var == true)
{
}
if ($var == false)
{
}
?>
you can have as many if statements as you want.
now let me show you how to embed php in html further.
<html>
<body>
<?php
//This is a comment
/*
This is
a comment
block
*/
?>
</body>
</html>
thats how it would look to put php in a html file now there are such things called comments and are used frequently now to open a multi line comment you use a slash and a start like so /* to close it you write the opposite of that like so */
You can do single line comments by using two slashes like so.... //
lets go a little deeper here little further then complete basics.
You can get the length of strings by using strlen() let me show you how with a example...
<?php
echo strlen("i wonder how many chars are in this sentance.");
?>
That is how to use strlen() to get the length of a string.
Lets show you The strpos() function.
The strpos() function is used to search for character within a string.
If a match is found, this function will return the position of the first match. If no match is found, it will return FALSE.
Let's see if we can find the string "world" in our string:
<?php
echo strpos("Hello world!","world");
?>
The output of the code above will be:
6
The position of the string "world" in our string is position 6. The reason that it is 6 (and not 7), is that the first position in the string is 0, and not 1.
Now here is a list of all operators i want you to study this for the little bit of time we have left.
and study it for homework tomarow we will be learning about soem functions and creating functions.
This is the url to study and only study this page http://www.w3schools.com/PHP/php_operators.asp