标签:ast learn input sorted return unless const sch types
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 $x echo "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.
----
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
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: here
----
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 function func($val=40) //func with opt parm val, defaults to 40 {} func(); //call it ?>
---
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 "<br>"; }
array can be called recursively to create multi-level array
complete array intro is here.
sorting an array in different ways: sort, rsort, asort, ksort, arsort, krsort.. indexed/associative arrays are sorted using different functions
---
php globals
$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 here.
$_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
标签:ast learn input sorted return unless const sch types
原文地址:http://www.cnblogs.com/sansna/p/7891754.html