Author Topic: PHP tutorials  (Read 2402 times)

Offline kingW3

  • Partimanijak
  • Retired Moderator
  • MonsterKill Member
  • *
  • Topic Author
  • Posts: 2773
  • Country: cs
  • Karma: +219/-196
  • Awards GFX staff member [RARE]
    • View Profile
    • Awards
PHP tutorials
« on: October 10, 2012, 19:38 »
Is anyone interested in PHP tutorials?If anyone wants i'll start making them
« Last Edit: October 10, 2012, 19:52 by kingW3 »

50Euro

  • Guest
Re: PHP tutorials
« Reply #1 on: October 10, 2012, 20:46 »
yeah please do it

Offline kingW3

  • Partimanijak
  • Retired Moderator
  • MonsterKill Member
  • *
  • Topic Author
  • Posts: 2773
  • Country: cs
  • Karma: +219/-196
  • Awards GFX staff member [RARE]
    • View Profile
    • Awards
Re: PHP tutorials
« Reply #2 on: October 11, 2012, 09:56 »
1. Basics
1.1 Basics
PHP 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 Variables

Variables 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 text
1.3 Data Types
1.3.1 Strings
Strings can be single quoted and double quoted.Single quoted strings only treat \' and \\
while double-quotes threat all these signs
Spoiler for from php.net:
\nlinefeed (LF or 0x0A (10) in ASCII)
\rcarriage return (CR or 0x0D (13) in ASCII)
\thorizontal tab (HT or 0x09 (9) in ASCII)
\vvertical tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5)
\eescape (ESC or 0x1B (27) in ASCII) (since PHP 5.4.0)
\fform feed (FF or 0x0C (12) in ASCII) (since PHP 5.2.5)
\\backslash
\$dollar sign
\"double-quote
\[0-7]{1,3}        the sequence of characters matching the regular expression is a
        character in octal notation
       
\x[0-9A-Fa-f]{1,2}        the sequence of characters matching the regular expression is a
        character in hexadecimal notation
       
Examples for single quote from php.net also(since it's the best)

<?php
echo &#39;this is a simple string&#39;;

echo &#39;You can also have embedded newlines in 
strings this way as it is
okay to 
do&#39;;

// Outputs: Arnold once said: "I&#39;ll be back"
echo &#39;Arnold once said: "I\&#39;ll be back"&#39;;

// Outputs: You deleted C:\*.*?
echo &#39;You deleted C:\\*.*?&#39;;

// Outputs: You deleted C:\*.*?
echo &#39;You deleted C:\*.*?&#39;;

// Outputs: This will not expand: \n a newline
echo &#39;This will not expand: \n a newline&#39;;

// Outputs: Variables do not $expand $either
echo &#39;Variables do not $expand $either&#39;;
?>

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&#39;s a \ and would generate a E_NOTICE because $5bucks variable isn&#39;t defined
*/

1.3.2 [size=10pt]Booleans[/size]
This is the simplest data type,it consists from 1 and 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 Integers
Those 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 Float
Floating 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 
"10.5";                // $foo is float (11.5)
$foo "-1.3e3";              // $foo is float (-1299)
$foo "bob-1.3e3";           // $foo is integer (1)
$foo "bob3";                // $foo is integer (1)
$foo "10 Small Pigs";       // $foo is integer (11)
$foo "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 Arrays
Arrays divide an variable into multiply elements like this

$a 
= array(
"bar => "foo",
1 => 2,
);
echo 
$a[&#39;bar&#39;]; // will output foo, i mostly use it like this
$b = array();
$b[&#39;bar&#39;] = "foo";

I'm tired now,i'll explain object when i finish the procedural style
To be continued
« Last Edit: October 13, 2012, 13:06 by kingW3 »

Offline bukka

  • GFX Moderator
  • Retired Moderator
  • Pro Member
  • *
  • Posts: 348
  • Karma: +67/-12
  • "Time is mana"
  • Awards GFX staff member [RARE]
    • View Profile
    • Pixel Laboratories
    • Awards
Re: PHP tutorials
« Reply #3 on: October 12, 2012, 23:14 »
Good job, initially I wanted to suggest you to use the code tags. But after realizing how awful they look I had to take that back.

Definitely needs a rework (different extension).

Just look at it:
Code: [Select]
Random Code
Anyways, keep it up, I am going to join you on this course.

Offline kingW3

  • Partimanijak
  • Retired Moderator
  • MonsterKill Member
  • *
  • Topic Author
  • Posts: 2773
  • Country: cs
  • Karma: +219/-196
  • Awards GFX staff member [RARE]
    • View Profile
    • Awards
Re: PHP tutorials
« Reply #4 on: October 13, 2012, 11:28 »
I was thinking of it(for example simplemachines forum have way better appereance of the code tag) i guess i'll just use the [php] tag even though it looks ugly here,also if anyone finds grammar error or a typo feel free to report,btw added 1.3
« Last Edit: October 13, 2012, 13:06 by kingW3 »