PHP速学
基本代码
<?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");
?>
魔术常量
name | Description |
---|---|
__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速学的更多相关文章
- Linux之:Ubuntu速学笔记(1)
撰写日期:2016-7-2 17:11:28 Saturday 课程资源: web程序员角度ubuntu自修速学课程 链接来源:程序员在囧途, VMware: VMware Workstation1 ...
- 沈逸老师ubuntu速学笔记(2)-- ubuntu16.04下 apache2.4和php7结合编译安装,并安裝PDOmysql扩展
1.编译安装apache2.4.20 第一步: ./configure --prefix=/usr/local/httpd --enable-so 第二步: make 第三步: sudo make i ...
- 沈逸老师ubuntu速学笔记(1)--安装flashplayer,配置中文输入法以及常用命令
开篇首先感谢程序员在囧途(www.jtthink.com)以及沈逸老师,此主题笔记主要来源于沈老师课程.同时也感谢少年郎,秦少.花旦等同学分享大家的学习笔记. 1.安装flash player ctr ...
- 快看Sample代码,速学Swift语言(1)-语法速览
Swift是苹果推出的一个比较新的语言,它除了借鉴语言如C#.Java等内容外,好像还采用了很多JavaScript脚本里面的一些脚本语法,用起来感觉非常棒,作为一个使用C#多年的技术控,对这种比较超 ...
- React Native之React速学教程(下)
概述 本篇为<React Native之React速学教程>的最后一篇.本篇将带着大家一起认识ES6,学习在开发中常用的一些ES6的新特性,以及ES6与ES5的区别,解决大家在学习Reac ...
- React Native之React速学教程(中)
概述 本篇为<React Native之React速学教程>的第一篇.本篇将从React的特点.如何使用React.JSX语法.组件(Component)以及组件的属性,状态等方面进行讲解 ...
- React Native之React速学教程(上)
概述 本篇为<React Native之React速学教程>的第一篇.本篇将从React的特点.如何使用React.JSX语法.组件(Component)以及组件的属性,状态等方面进行讲解 ...
- 突破,Objective-C开发速学手册
<突破,Objective-C开发速学手册> 基本信息 作者: 傅志辉 出版社:电子工业出版社 ISBN:9787121207426 上架时间:2013-7-12 出版日期:2013 年8 ...
- 快看Sample代码,速学Swift语言(2)-基础介绍 快看Sample代码,速学Swift语言(1)-语法速览
快看Sample代码,速学Swift语言(2)-基础介绍 Swift语言是一个新的编程语言,用于iOS, macOS, watchOS, 和 tvOS的开发,不过Swift很多部分内容,我们可以从C或 ...
- grpc之protobuf常用语法速学
1,语法速学(1):返回商品”数组”.repeated修饰符 Repeated:是一个修饰符,返回字段可以重复任意多次(包括0次) 可以认为就是一个数组(切片) 服务端: 创建protobuf文件 s ...
随机推荐
- [SinGuLaRiTy] 高一下半期测试
[SinGuLaRiTy-1017] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 对于所有的题目: Time Limit: 1s | Me ...
- CSS之字体样式
css字体样式 font-size:字号大小 font-size属性用于设置字号,该属性的值可以使用相对长度单位,也可以使用绝对长度单位.其中,相对长度单位比较常用,推荐使用像素单位px,绝对单位使用 ...
- 读懂sql_trace
TKPROF: Release 11.2.0.1.0 - Development on 星期一 11月 3 21:39:41 2014 Copyright (c) 1982, 2009, Oracle ...
- 解决Android应用安装快完毕时提示签名冲突,android签名文件生成(转)
最近开发了一个Android手机应用,自己用Eclipse调试安装没问题,使用其他人调试生成的bin下的apk就会出现问题,安装到最后提示"安装签名冲突"错误,想了一下估计是没有给 ...
- 一些意想不到的小bug。
一,当if的时候,很容易忘记相对的else,从而出现bug,要将背面消息考虑全面. 二,多个元素在同一行布局的时候,要考虑文字的换行还是省略号代替. 例如:左边一个文字,宽度不固定,右边一个图形,宽度 ...
- umask码和文件权限
umask是chmod配套的,总共为4位(gid/uid,属主,组权,其它用户的权限),不过通常用到的是后3个,例如你用chmod 755 file(此时这文件的权限是属主读(4)+写(2)+执行(1 ...
- mac 卸载 node
sudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,share/man/*/node.*} 除此之外,还需要检查一 ...
- Qt 学习之路 2(24):Qt 绘制系统简介
Qt 学习之路 2(24):Qt 绘制系统简介 豆子 2012年10月30日 Qt 学习之路 2 77条评论 Qt 的绘图系统允许使用相同的 API 在屏幕和其它打印设备上进行绘制.整个绘图系统基于Q ...
- 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_dynamic基元类型
1.代码使用 dynamic 表达式/变量来调用一个成员时,编译器会生成特殊的 IL 代码来描述所需的操作.这种特殊的代码称为 payload(有效载荷).在运行时,payload 代码根据当前由 d ...
- UESTC 1437
LCA模板题 随便找的倍增模板... #include<bits/stdc++.h> using namespace std; const int maxn = 1e5+11; int t ...