1. Basics1.1
BasicsPHP files use those file extensions .php, .phtml, .php4 .php3, .php5, .phps
.php is the most common extension used.
Comments in php are written with a // before the text and /* for multiple lines and they are enclosed with */ like this
//This is a comment
/* This
is
a
multiline
comment
*/ In order that browser treats PHP code as php it has to be opened and closed with following <?php and ?>
While <? and ?> can also be used it's not recommendable.
After declaring a variable or doing a function you need to put ; at the end(doesn't apply to if,for,while,foreach,else loops)
1.2
VariablesVariables in PHP can store values of anything(strings,integers,objects,booleans,arrays,resources explained later) and are used if the same value has to be entered again.
They are declared by a dollar sign($) and their value is added with a equal(=) sign and the right side becomes the value of the left side,like this
<?php
$number = 135; // $number is equal to 135
$number1 = 31; // $number1 is equal to 31
$number2 = 53; // $number2 is equal to 53
$number3 = $number - $number1;
//The value of $number3 is 135-31 or 104 which is the same
?>
NOTE Data Types is a hard lecture,i copied a lot of text from php.net you'll see notices in above text1.3
Data Types1.3.1
StringsStrings can be single quoted and double quoted.Single quoted strings only treat \' and \\while double-quotes threat all these signsExamples for single quote from php.net also(since it's the best)
<?php
echo 'this is a simple string';
echo 'You can also have embedded newlines in
strings this way as it is
okay to do';
// Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\\*.*?';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\*.*?';
// Outputs: This will not expand: \n a newline
echo 'This will not expand: \n a newline';
// Outputs: Variables do not $expand $either
echo 'Variables do not $expand $either';
?>
For double quotes
echo "Socrates said "the only real wisdom is knowing you know nothing " and he was right ";
//Outputs a Error because the echo statement was closed by " this is how it should be done
echo "Socrates said \"the only real wisdom is knowing you know nothing\" and he was right\n<br>";
//Outputs Socrates said "the only real wisdom is knowing you know nothing" and he was right";
echo " C:\\ is the root directory of OS and i paid it $5bucks"
/*Outputs C:\ is the root directory of OS and i paid it because \\ is treated as escaping character so it's a \ and would generate a E_NOTICE because $5bucks variable isn't defined
*/
1.3.2 [size=10pt]Booleans[/size]
This is the simplest data type,it consists from 1 and 0 or TRUE and FALSE for false you can set it like this
[php]
$s = FALSE;
$s = 0;
$s = 0.0;
$s = "";
$s = "0";
$s = NULL;
$s = array();
everything other will return true
1.3.3
IntegersThose are basically all real numbers,for example, 21, 4, and −2048 are integers; 9.75, 5½, and √2 are not integers.
Integer boundaries on 32-bit system is 2147483647,2147483648 will return float as data type
on 64-bit system
9223372036854775807 and 9223372036854775808 will return float 2 examples from php.net(first one is 32 other 64)
<?php
$large_number = 2147483647;
var_dump($large_number); // int(2147483647)
$large_number = 2147483648;
var_dump($large_number); // float(2147483648)
$million = 1000000;
$large_number = 50000 * $million;
var_dump($large_number); // float(50000000000)
?>
64-bit
<?php
$large_number = 9223372036854775807;
var_dump($large_number); // int(9223372036854775807)
$large_number = 9223372036854775808;
var_dump($large_number); // float(9.2233720368548E+18)
$million = 1000000;
$large_number = 50000000000000 * $million;
var_dump($large_number); // float(5.0E+19)
?>
1.3.4
FloatFloating point numbers (also known as "floats", "doubles", or "real numbers") can be specified using any of the following syntaxes:
<?php
$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
?>
Float numbers are not precise
Spoiler for php.net:
Floating point numbers have limited precision. Although it depends on the system, PHP typically uses the IEEE 754 double precision format, which will give a maximum relative error due to rounding in the order of 1.11e-16. Non elementary arithmetic operations may give larger errors, and, of course, error propagation must be considered when several operations are compounded.
Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2, which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118....
So never trust floating number results to the last digit, and do not compare floating point numbers directly for equality. If higher precision is necessary, the
arbitrary precision math functions and
gmp functions are available.
String conversion to numbers(php.net)
<?php
$foo = 1 + "10.5"; // $foo is float (11.5)
$foo = 1 + "-1.3e3"; // $foo is float (-1299)
$foo = 1 + "bob-1.3e3"; // $foo is integer (1)
$foo = 1 + "bob3"; // $foo is integer (1)
$foo = 1 + "10 Small Pigs"; // $foo is integer (11)
$foo = 4 + "10.2 Little Piggies"; // $foo is float (14.2)
$foo = "10.0 pigs " + 1; // $foo is float (11)
$foo = "10.0 pigs " + 1.0; // $foo is float (11)
?>
1.3.5
ArraysArrays divide an variable into multiply elements like this
$a = array(
"bar => "foo",
1 => 2,
);
echo $a['bar']; // will output foo, i mostly use it like this
$b = array();
$b['bar'] = "foo";
I'm tired now,i'll explain object when i finish the procedural style
To be continued