PHP: Learning Notes

from here.

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.

to include scripts from other php file:

<?php include 'path/to/php.php'; ?>

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: 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
  • $_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 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

----

php file operations

$file = fopen("filename","r");

while(!feof($myfile)) {
  echo fgets($myfile) . "<br>";
}

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 here.

----

php cookie

setcookie(name, value, expire, path, domain, secure, httponly);

isset($_COOKIE[$cookie_name])

----

php session

<?php
session_start(); // This should be put at the very first place before any possible output
?>
<!DOCTYPE html>
<html>
<body>

<?php
// Set session variables
$_SESSION["favcolor"] = "green";
$_SESSION["favanimal"] = "cat";

// to change a session variable, just overwrite it 
$_SESSION["favcolor"] = "yellow";

// Print all session variables
print_r($_SESSION);

// Echo session variables that were set on previous page
echo "Favorite color is " . $_SESSION["favcolor"] . ".<br>";
echo "Favorite animal is " . $_SESSION["favanimal"] . ".";

// remove all session variables
session_unset(); 

// destroy the session 
session_destroy(); 

?>

 

posted on 2017-11-24 17:57  三叁  阅读(180)  评论(0编辑  收藏  举报

导航