PHP MYSQL In 8 Hours By Ray Yao For Beginners Learn PHP Fast! Copyright 2015 by Ray Yao All Rights Reserved Neither part of this e-book nor whole of this e-book may be reproduced or transmitted in any form or by any means electronic, photographic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without prior written permission from the author. Ray Yao About the Author Ray Yao: Certified PHP engineer by Zend, USA Certified JAVA programmer by Sun, USA Certified SCWCD developer by Oracle, USA Certified A+ professional by CompTIA, USA Certified ASP. NET expert by Microsoft, USA Certified MCP professional by Microsoft, USA Certified TECHNOLOGY specialist by Microsoft, USA Certified NETWORK+ professional by CompTIA, USA Ray Yaos books: Linux Command Line JAVA 100 Tests for interview PHP 100 Tests for Interview JAVA in 8 Hours PHP in 8 Hours JavaScript in 8 Hours C++ in 8 Hours Ray Yao's books Preface PHP & MYSQL in 8 Hours is a useful book for beginners. You can learn complete primary knowledge of PHP & MYSQL fast and easily. The straightforward definitions, the plain and simple examples, the elaborate explanations and the neat and beautiful layout feature this helpful and educative book. You will be impressed by the new distinctive composing style.
Reading this book is a great enjoyment! You can master all essential PHP & MySQL skill in 8 hours. Start Coding Today! (The appendix of this book includes (1) 6 Security Tips (2) MySQL Command Charts (3) 100 Tests for Interview, Answers and Explanations) Table of Contents
Start PHP
What is PHP? PHP is a server-side language for web development, meaning Personal Homepage Program. The syntax of PHP looks like this:
Example:
Explanation:
PHP code tag begins with echo is command of PHP, meaning output or display. echo (Hello World); will output Hello World. PHP Reserved Words Some words are only used by PHP language itself. They may not be used when choosing identifier names for variable, function, and labels.
The following words are part of PHP reserved words
and | array( ) | break |
case | const | continue |
default | do | echo( ) |
else | else if | empty( ) |
eval( ) | exception | exit( ) |
extends | false | for |
function | foreach | global |
if | include( ) | include_once( ) |
isset( ) | list( ) | new |
null | or | print( ) |
require( ) | require_once( ) | return( ) |
static | switch | true |
unset( ) | xor | while |
Example:
const // const is a reserved word. continue // continue is a reserved word.
Explanation:
PHP reserved words may be not used as variable name, function name, and label name. Comments // is used in single-line comments. /**/ are used in multi-line comments.
Example:
Explanation:
// output OK; is a single-line comment. /* echo (OK); is a PHP statement, echo is a keyword, it display a word OK. /* echo (OK); is a PHP statement, echo is a keyword, it display a word OK.
PHP is very easy to learn.*/ is a multi-line comment. Variables Variable is a symbolic name associated with a value. Variable name starts with $ symbol.
Example:
Explanation:
$var, $abcde and $my_variable are all variables. They can store some value. $var=100; $abcde=Hello World; $my_variable=true; Notice that variable naming cannot start with number, cannot have space. e.g. $23var, $abc de are invalid variable name. $23var, $abc de are invalid variable name.
But $var23, $abcde are valid variable name. Data Types String characters within double or single quotation. Integer numbers without decimal point. Boolean a value with true or false. Float numbers with decimal point. $int=168; // $int data type is Integer. $float=12.88; // $float data type is Float. $bool=true; // $bool data type is Boolean. $bool=true; // $bool data type is Boolean.
Note: Double quotes or single quotes are always used in string. e.g. abcde, learning. Escaping Characters The \ backslash character can be used to escape characters. \n outputs content to the next new line. \ outputs a double quotation mark.
Example:
Explanation:
\ outputs a double quotation mark.
Example:
Explanation:
\ outputs a double quotation mark.
Note that Hello World has a double quotation with it. Another sample: \t makes a tab echo Hello \t\t\t World; // Note here has three taps ( Output: Hello World ) Functions A function is a code block that can repeat to run many times. To define a function, use function function-name ( ) { }.
function function-name ( ) {} |
To call a function, use function-name ( );
Example:
Explanation:
function test( ) { } is a declaration of PHP test( ) function. test( ); is a command to call a function named test( ){ }. When running the function test ( ), it will output show a sample.
Function with Arguments A function can have one or more arguments inside the bracket. Arguments are used to pass data to function.
function function-name ( $argument ){} |
Example:
Explanation:
$arg is an argument of function test($arg ){ }. When test(display a sample) call the function test($arg){ }, it will pass the display a sample to $arg. After $arg has received the data, it will pass the data inside the function. echo ($arg) will use the data, and then outputs display a sample.
Variable Scope Global variable is declared outside the function, it can be used in everywhere; local variable is declared inside the function, it is only used inside current function.
Example:
Explanation:
echo $num outputs 200, which means $num store a value of global variable. If you want to declare a global variable inside a function, you should use a keyword global preceding the variable name. e.g. function test( ) { global $num; } Multiple Arguments A function may have several arguments; these arguments will pass data simultaneously for the function.
function function-name ( $arg1, $arg2, $arg3){} |
Example:
Explanation:
When test(3, 6, 9) call the function test ($a, $b, $c){}, arguments 3,6,9 will pass to the $a, $b, $c, the $sum will be assigned the value of 18.
The output is 18.
PHP Basic
Arithmetical Operators
Operators | Running |
+ | add |
- | subtract |
* | multiply |
/ | divide |
% | get modulus |
++ | increase 1 |
- - | decrease 1 |
. | connect two strings |
Next page