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 ...
随机推荐
- android listview addHeaderView和addFooterView的注意事项
1. item内如果有button等控件时,在监听listview的onitemclick事件时,焦点会被item内的button. imagebutton等控件抢走,从而导致在listview设置了 ...
- C++11新标准:auto关键字
一.auto意义 编程时常常需要把表达式的值赋给变量,这就要求在声明变量的时候清楚地知道表达式的类型,然后要做到这一点并非那么容易.为了解决这个问题,C++11新标准引入了auto类型说明符,用它就能 ...
- .net core 基于NPOI 的excel导入导出类,支持自定义导出哪些字段,和判断导入是否有失败的记录
#region 从Excel导入 //用法 //var cellHeader = new Dictionary<string, string>(); //cellHeader.Add(&q ...
- 工作中用的cobbler命令行
在使用cobbler服务器,从pxe启动虚机的时候,经常用到的cobbler命令行 1.查看注册信息 cobbler system report --name=test25 2.注册信息 cobble ...
- fopen_s()
原型:errno_t fopen_s( FILE** pFile, const char *filename, const char *mode ); 例子: char *filePath=&q ...
- git rm 与 git reset
https://www.cnblogs.com/sunshine-xin/articles/3521481.html 1. git rm --cached file will remove the f ...
- 「BZOJ1010」[HNOI2008] 玩具装箱toy(斜率优化)
P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为 1⋯N1\cdots N1⋯N ...
- Bicoloring UVA - 10004 二分图判断
\(\color{#0066ff}{题目描述}\) 多组数据,n=0结束,每次一个n,m,之后是边,问你是不是二分图 \(\color{#0066ff}{输入样例}\) 3 3 0 1 1 2 2 0 ...
- NOIP2018初赛总结(提高组)(试题+答案+简要解析)
NOIP2018初赛总结(提高组) 更新完毕(纯手敲),如果有错误请在下面留言 单选题 T1.下列四个不同进制的数中,与其它三项数值上不相等的是 A.\((269)_{16}\) B.\((617)_ ...
- [转载]C#实现获取浏览器信息
原文地址:C#实现获取浏览器信息作者:flywithme Request.Browser.MajorVersion.ToString();//获取客户端浏览器的(主)版本号 Request.Bro ...