from .
php basics
in PHP, variables are defined as $_alphanum
variables should always prefixed a $
variables defined/assigned outside of a function cannot be used inside a function, unless declared inside function with keyword global $var. Also can be used as $GLOBALS['var'] for in/out.
echo variables: (all of the 3 examples are okay)
echo $xecho "x is equal to $x"echo "x is equal to " . $x
comments can be:
# comments// comments/* comments */
the static keyword inside function keeps the variable change everytime calls the function.
to include scripts from other php file:
require is basically the same as include, except for its not stop running following scripts when not finding the mentioned php file.
----
php functions
echo: what echo echoes is pure-html, can be tagged htmls. echo strings concat using ., echo variables can do math ops. echo multiple elements separated by ,
print: print only accept one element, returns 1, slower
---
php types
- String
- Integer
- Float (floating point numbers - also called double)
- Boolean
- Array
- Object
- NULL
- Resource
var_dump($var) function dumps a var together with its type
bool: false true
create arrays by array() constructor. $x= array("a","b","c")
object are instances of class, class can be defined by:
class Car { function Car() { $this->model = "VW"; }}
instantialize it to object by new:
$herbie = new Car();
any var created without init is as null.
resource type: relating to db calls
---
php string manipulating
strlen(): counts to last '\0'
str_word_count(): counts words
strrev(), reverse a string str->rts
strpos(str, pattern), returns start of pattern idx in str
str_replace(patt, rep, str) returns str's all pattern replaced by rep
complete list of string funcs:
----
php constants
define(name,value[,opt-case-sensitive])
----
php arith
===, ==, !=, !==, ++, --, and(&&), or(||), xor, !,
.=: str concat
---
php cond
if, switch($var)->case:->break->default:
---
php function
---
php array
count($arr) returns count of array elems
array can also be created in associative way:
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");foreach($age as $x => $x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "";}
array can be called recursively to create multi-level array
complete array intro is .
sorting an array in different ways: sort, rsort, asort, ksort, arsort, krsort.. indexed/associative arrays are sorted using different functions
---
php globals
- $GLOBALS
- $_SERVER
- $_REQUEST
- $_POST
- $_GET
- $_FILES
- $_ENV
- $_COOKIE
- $_SESSION
$GLOBALS is introd before
$_SERVER['HTTP_USER_AGENT'] returns user-agent field of the incoming request. other fields can be PHP_SELF, SERVER_NAME, HTTP_HOST, SCRIPT_NAME, REQUEST_METHOD.. more to look at .
$_REQUEST['varname'] is used to collect the value of the input field from a form.
$_POST['varname'] is used to collect the value of a post http request with key=varname
$_GET['varname'] is used to collect the value of a request which has uri: domain.com/page.html?varname=value
post key-values may be embedded in ssl/tls, so its sneaker than get option
----
php file operations
$file = fopen("filename","r");while(!feof($myfile)) { echo fgets($myfile) . "";}fclose($file);
fgets get one line at a time, fgetc get one char at a time.
after configuring php.ini: file_uploads = On
we can upload file as .
----
php cookie
setcookie(name, value, expire, path, domain, secure, httponly);isset($_COOKIE[$cookie_name])
----
php session
";echo "Favorite animal is " . $_SESSION["favanimal"] . ".";// remove all session variablessession_unset(); // destroy the session session_destroy(); ?>