您的位置:首页 > 编程语言 > PHP开发

PHP编程入门的基本语法知识点总结

2016-01-26 17:39 841 查看

一、何为php

    PHP,即“PHP: Hypertext   Preprocessor”,是一种被广泛应用的开源通用脚本语言,尤其适用于 Web 开发并可嵌入 HTML   中去。它的语法利用了 C、Java 和 Perl,易于学习。该语言的主要目标是允许 web 开发人员快速编写动态生成的 web   页面,但 PHP 的用途远不只于此。

    简单来说,就是php是一种脚本语言,可以做很多事情。①服务器端脚本 ②命令行脚本 ③编写桌面程序

二、开始php

    (1)下载php解释器,其实win下面,最简单的还是wamp这个软件,下载下来什么都有了...

    (2)win下面貌似还需要,mscvr110.dll 这个链接库,vc2012运行库,安装即可

    (3)ide,无耻的使用了 phpStorm,等哥有钱了一定给你补回来, so...

User: newasp
License:
===== LICENSE BEGIN =====
14617-12042010
00001xrVkhnPuM!Bd!vYtgydcusnqt
mM!hZWoGg"DprWxZCBwsy8T91O7MRu
NVHtrbzv8O9mmoLvtijcHSSE7i5Jr!
===== LICENSE END ====

三、入门引导

    (1)简单的输出

<?php
/**
* Created by PhpStorm.
* User: LENOVO
* Date: 2014/9/28
* Time: 14:51
*/
// 输出PHP详细信息
echo phpinfo();
//C:\php-5.6.1-Win32-VC11-x86\php.exe D:\dizzy\php_test\index.php
//phpinfo()
//PHP Version => 5.6.1
//
//System => Windows NT LENOVO-PC 6.1 build 7600 (Windows 7 Ultimate Edition) i586
//Build Date => Sep 24 2014 18:54:12
//Compiler => MSVC11 (Visual C++ 2012)
//Architecture => x86
//Configure Command => cscript /nologo configure.js "--enable-snapshot-build" "--disable-isapi" "--enable-debug-pack" "--without-mssql" "--without-pdo-mssql" "--without-pi3web" "--with-pdo-oci=c:\php-sdk\oracle\x86\instantclient_12_1\sdk,shared" "--with-oci8-12c=c:\php-sdk\oracle\x86\instantclient_12_1\sdk,shared" "--enable-object-out-dir=../obj/" "--enable-com-dotnet=shared" "--with-mcrypt=static" "--without-analyzer" "--with-pgo"
//Server API => Command Line Interface

    (2)简单的表单处理

// 一个简单的html表单
<form action="action.php" method="post">
<p>姓名: <input type="text" name="name" /></p>
<p>年龄: <input type="text" name="age" /></p>
<p><input type="submit" /></p>
</form>
// action.php 接收表单数据, 使用超全局变量
%_POST["name"]
%_POST["age"]
<?php echo htmlspecialchars($_POST['name']); ?>
<?php echo (int)$_POST['age']; ?>
// 这便是最简单的表单提交,及数据接收

四、基本语法

    (1)PHP标记

<?php
echo "Hello World!";
// 当文件为纯PHP时,最好在末尾删除PHP结束标记
//?>

    (2)从HTML中分离

// 在一对开始和结束之外的内容,都会被PHP解释器忽略。也就是html标签和PHP代码混合的那种,跟jsp,asp一样...
<p>This is going to be ignored by PHP and displayed by the browser.</p>
<?php echo 'While this is going to be parsed.'; ?>
<p>This will also be ignored by PHP and displayed by the browser.</p>
// 使用条件,高级分离
<?php if ($expression == true): ?>
This will show if the expression is true.
<?php else: ?>
Otherwise this will show.
<?php endif; ?>

    (3)指令分隔符,注释

        PHP需要在每个语句后面用分隔符结束指令。

        注释: // 或 /* ... */  但是,*/ 会匹配最近的那个,切记!切记!

五、类型

    PHP支持8种原始数据类型。

  •     四种标量类型:boolean(布尔型),integer(整型),float(浮点型,double),string(字符串)
  •     两种复合类型:array(数组),object(对象)
  •     两种特殊类型:resource(资源),NULL(无类型)
<?php
$a_bool = TRUE;  // a boolean
$a_str = "foo"; // a string
$a_str2 = 'foo'; // a string
$an_int = 12;   // an integer
echo gettype($a_bool); // prints out: boolean
echo gettype($a_str); // prints out: string
// If this is an integer, increment it by four
if (is_int($an_int)) {
$an_int += 4;
}
// If $bool is a string, print it out
// (does not print out anything)
if (is_string($a_bool)) {
echo "String: $a_bool";
}
?>

    (1)Boolean 布尔类型

        可以为TRUE或FALSE,不区分大小写。

        一般非0,即为TRUE。

    (2)Integer 整型

        整型可以使用十进制,十六进制,八进制或二进制表示。八进制前面必须加0(零),十六进制加0x,二进制加0b。

        如果给定的一个数超出了interger的范围,将会被解释为float。同样运算结果超出integer范围,同样如此。

        php没有整除运算符,1/2 将产生出 float 0.5。可以强制转换为integer 或使用round() 更好的四舍五入。

echo (int)2.9; // 输出 2
echo round(2.555, 2) // 输出 2.56
// 决不要将未知的分数强制转换为 integer,这样有时会导致不可预料的结果。
<?php
echo (int) ( (0.1+0.7) * 10 ); // 显示 7!
?>

    (3)Float 浮点型(double)

        浮点型,也叫浮点数float,双精度double, 实数real。

<?php
$a = 1.234;
$b = 1.2e3;
$c = 7E-10;
?>

    (4)String 字符转

        一个字符串string,就是由一系列的字符组成,其中每个字符等同于一个字节。这就意味着php只能支持256个字符集,因此不支持Unicode。

        string最大可以达到2GB。

<?php
$a = 123;
echo '$a'; // 输出 $a
echo "$a"; // 输出 123, 转义字符 '\'
$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;
?>

    (5)Array 数组

        php中的数组,实际上是一个有序序列。映射是把values关联到keys的类型。

        由于数组元素的值也可以说是另外的数组,树形结构和多维数组也是允许的。

<?php
$array = array(
"foo" => "bar",
"bar" => "foo",
);
// 自PHP 5.4 起
$array = [
"foo" => "bar",
"bar" => "foo",
]
// key 可以是 integer 或 string 类型
// key 值为可选项, 如果未指定,则使用之前用过最大的integer键名加上1作为新键名
?>
// 要修改某个值,通过其键名给该单元赋一个新值。
// 要删除某个键值对,对其调用 unset() 函数。

        使用 unset() 需要注意,此时数组不会重建索引。需要重建索引,可以使用 array_values() 函数。

        数组计算总数: 使用 count() 函数

    (6)Object 对象

<?php
class foo{
function do_foo(){
echo "Doing foo.";
}
}
// 用 new 实例化一个类
$f = new foo;
$f->do_foo;

    (7)Resource 资源类型

        资源 resource 是一种特殊的变量,保存了到外部资源的一个引用。资源是通过专门的函数来建立和使用的。

    (8)NULL

        特殊的NULL 表示一个变量没有值。NULL类型唯一可能的值就是NULL。

        可被认定为NULL的变量:①被赋值为NULL ②尚未被赋值 ③被unset

    (9)Callback 回调类型

        自PHP5.4 起,可以使用 callable 类型 指定回调类型 callback。

六、变量

    php中变量用一个美元符号 $ 后面跟变量名来表示的。区分大小写。

    变量默认总是传值赋值。

<?php
$a = 1;
// 值传递赋值
$b = $a
// 引用赋值
$c = &$a
// global 关键字
global ; $GLOBALS


您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PHP 语法