码迷,mamicode.com
首页 > Web开发 > 详细

PHP速学

时间:2016-01-13 00:32:12      阅读:261      评论:0      收藏:0      [点我收藏+]

标签:

基本代码

<?php
echo "Hello world";
?>

变量定义

<?php
$a=true;
$bool_value=true;
$integer_value=9;
$float_value=3.1415926;
$string_value="Pi_is_{{$float_value}}.";
echo $string_value;//Pi_is_{3.1415926}.
?>

输出

<?php
$str="string_";
$return_value=print($str);//success: return 1, faile: return 0
echo $return_value;//no return value
$return_value=printf("value:%f",3.1415926);
$str=sprintf("value:%f",3.1415926);//print to variable str
echo $str;
?>

数据结构-数组

<?php
$season[0]=‘spring‘;
$season[1]=‘summer‘;
$season[2]=‘autumn‘;
$season[3]=‘winter‘;

$map[‘key1‘]=‘value1‘;
$map[‘key2‘]=‘value2‘;

//season & map are all array
echo $season;
echo $map;
?>

数据结构-对象

<?php
class Point
{
    private $id=0;
    public $x,$y;
    function __construct($x,$y)
    {
        $this->x=$x;
        $this->y=$y;
    }
    function Print_info()
    {
        echo $this->id,"<br>";
        echo $this->x,"<br>";
        echo $this->y,"<br>";
    }
}
$p=new Point(1,3);
$p->Print_info();
?>

数据结构-资源数据类型

        类似于句柄的概念,使用完成后需要销毁。

数据结构-空类型

<?php
$uninitialized;
$null_var1=null;
$var="123";
unset($var);
//this three variable are null
?>

类型转换

<?php
//int or integer, float or double or real, string, array, object, bool or boolean
//if an string starts with number, it will be truncated to a number in arithmetic
//if an string starts with non-number, it will be zero in arithmetic
//it‘s ok to run "3.14abc"+6, so double can be neglectable
echo (double)"a3.1415926abc";

//intval, doubleval, floatval, floatval, strval
echo intval(3.1415926);

//var is supposed by array, boolean, float, integer or int, null, object, unknow, string
$value="3.1415926";
$return_value = settype($value,int);//success: 1
echo $value;
?>

变量

值传递/引用传递,可变变量

<?php
//by value
$int1=1;
$int2=int1;
$int2=5;
echo $int1,"<br>",$int2,"<br>";//1 5

//by reference
$int1=1;
$int2=& $int1;
$int2=5;
echo $int1,"<br>",$int2,"<br>";//5 5

//Variable variables: use variable value to define a variable named value
$sun="hot";
$$sun="moon";//equal to $hot="moon"
//${$sun} is eual to $hot
echo $sun,"<br>",${$sun},"<br>";
//user aliases
echo $hot,"<br>";
?>

超级全局变量SuperGlobals

变量销毁

        重新赋值

        unset()

常量

<?php
class Test
{
    //the scope is this class
    const NAME="100";
    function classN()
    {
        //user without $
        echo Test::NAME*312;
    }
}
//the scope is global and it can be used anywhere
define("SITE_GLOBAL","www.site.com");
?>

魔术常量

A few "magical" PHP constants
NameDescription
__LINE__ The current line number of the file.
__FILE__ The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned.
__DIR__ The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory.
__FUNCTION__ The function name.
__CLASS__ The class name. The class name includes the namespace it was declared in (e.g. Foo\Bar). Note that as of PHP 5.4 __CLASS__ works also in traits. When used in a trait method, __CLASS__ is the name of the class the trait is used in.
__TRAIT__ The trait name. The trait name includes the namespace it was declared in (e.g. Foo\Bar).
__METHOD__ The class method name.
__NAMESPACE__ The name of the current namespace.

特殊运算符

`: 反引号,相当于shell_exec()函数(安全模式只能使用函数),

<?php
echo `dir`;
?>

@:错误控制,放在表达式前,产生的错误被忽略。如果激活track_errors属性,错误存放在$php_errormsg变量中。

foreach

<?php
$season[0]=‘spring‘;
$season[1]=‘summer‘;
$season[2]=‘autumn‘;
$season[3]=‘winter‘;
foreach($season as $s)
{
    echo $s,"<br>";
}
?>

 

PHP速学

标签:

原文地址:http://www.cnblogs.com/qiusuo/p/5125977.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!