Home > php教程 > php手册 > body text

Summary of basic syntax knowledge points for getting started with PHP programming_php basics

WBOY
Release: 2016-05-16 08:59:51
Original
3181 people have browsed it

1. what is php

php, or "php: hypertext preprocessor", is a widely used open source general scripting language, especially suitable for web development and can be embedded in html. its syntax leverages c, java, and perl and is easy to learn. the main goal of the language is to allow web developers to quickly write dynamically generated web pages, but php can be used for much more than that.

to put it simply, php is a scripting language that can do many things. ① server-side script ② command line script ③ writing desktop program

2. start php

(1) download the php interpreter. in fact, under win, the simplest software is wamp. download it and you will have everything...

(2) it seems that you still need it under win. the mscvr110.dll link library and the vc2012 runtime library can be installed

(3)ide, i shamelessly used phpstorm, i will make it up to you when i get rich, so...

user: newasp
license:
===== license begin =====
14617-12042010
00001xrvkhnpum!bd!vytgydcusnqt
mm!hzwogg"dprwxzcbwsy8t91o7mru
nvhtrbzv8o9mmolvtijchsse7i5jr!
===== license end ====
Copy after login

3. getting started

(1) simple output

<?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
Copy after login

(2) simple form processing

// 一个简单的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']; ?>
// 这便是最简单的表单提交,及数据接收

Copy after login

4. basic grammar

(1) php tag

<?php
 
echo "hello world!";
 
// 当文件为纯php时,最好在末尾删除php结束标记
//?>
Copy after login

(2) separate from 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; ?>
Copy after login

(3) instruction separator, comment

php requires a delimiter to terminate the directive after each statement.

comments: // or /* ... */ however, */ will match the closest one, remember! remember!

5. type

php supports 8 primitive data types.

  • four scalar types: boolean (boolean), integer (integer), float (floating point, double), string (string)
  • two composite types: array (array), object (object)
  • two special types: resource (resource), null (no type)
<?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";
}
?>
Copy after login

(1) boolean boolean type

can be true or false and is not case sensitive.

generally, if it is not 0, it is true.

(2) integer type

integers can be represented in decimal, hexadecimal, octal or binary. the octal number must be preceded by 0 (zero), the hexadecimal number must be preceded by 0x, and the binary number must be preceded by 0b.

if a given number exceeds the range of interger, it will be interpreted as float. the same operation result exceeds the range of integer, and the same is true.

php does not have an integer division operator, 1/2 will produce float 0.5. you can cast to integer or use round() for better rounding.

echo (int)2.9; // 输出 2
echo round(2.555, 2) // 输出 2.56

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

Copy after login

(3) float floating point type (double)

floating point type, also called floating point number float, double precision double, real number real.

<?php
$a = 1.234;
$b = 1.2e3;
$c = 7e-10;
?>
Copy after login

(4) string character conversion

a string string is composed of a series of characters, where each character is equivalent to a byte. this means that php can only support 256 character sets and therefore does not support unicode.

the maximum string size can reach 2gb.

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

(5) array array

the array in php is actually an ordered sequence. mapping is a type that associates values ​​to keys.

since the values ​​of array elements can also be said to be other arrays, tree structures and multidimensional arrays are also allowed.

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

when using unset(), please note that the array will not be re-indexed at this time. if you need to rebuild the index, you can use the array_values() function.

count the total number of arrays: use the count() function

(6) object

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

(7) resource resource type

resource resource is a special variable that holds a reference to an external resource. resources are created and used through specialized functions.

(8) null

the special null indicates that a variable has no value. the only possible value of type null is null.

variables that can be recognized as null: ① assigned to null ② not yet assigned ③ unset

(9) callback callback type

since php5.4, you can use the callable type to specify the callback type callback.

6. variables

variables in php are represented by a dollar sign $ followed by the variable name. case sensitive.

variables are always assigned by value by default.

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


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!