Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the entry-level knowledge points of PHP

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Xiaobian to share with you what PHP entry knowledge points, I believe most people do not know how, so share this article for everyone's reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

I. The first condition

You must first have a working PHP enabled web server. I assume that all PHP files on your server have the extension.php3.

PHP installation

Generate a file named test.php3 that contains the following:

Then open the file in your browser. Take a look at this page to see what options your PHP installation uses.

III. Grammar

As mentioned earlier, you can mix PHP code with HTML code. So you have to have a way to tell the difference. Here are a few ways you can do it. You can choose one that suits you best and stick with it!

Detach from HTML

The following methods can be used:

. . .

statement

As with Perl and C, statements are separated in PHP by (;). Flags that are separate from HTML also indicate the end of a statement.

annotation

PHP supports C, C++ and Unix-style comments:

/* C,C++ style multiline comments */ // C++ style single-line comments #Unix-style single-line comments

Example: Hello,World!

With what we've learned, you can write the simplest program that outputs perhaps the most famous word in the programming world:

First PHP page

IV. Types of data

PHP supports integers, floating point numbers, strings, arrays, and objects. Variable types are usually not determined by the programmer but by PHP runtime (what a relief!). But the type can also be explicitly set by the cast or settype() function.

numerical

Numeric types can be integers or floating-point numbers. You can assign a value using the following statement:

$a = 1234; #decimal $a = -123; #negative $a = 0123; #octal number (equal to 83 decimal) $a = 0x12; #hexadecimal number (equal to 18 decimal) $a = 1.234; #Floating-point number "double precision number" $a = 1.2e3; #Exponential form of double precision numbers

string

Strings can be defined by fields enclosed in single quotes or double quotes. Note that the difference is that strings enclosed in single quotes are literally defined, while strings enclosed in double quotes can be extended. The backslash (\) can be used to separate certain special characters. Examples are as follows:

$first = 'Hello'; $second = "World"; $full1 = "$first $second"; #generate Hello World $full2 = '$first $second';#generate $first $second

Characters and numbers can be concatenated using operators. Characters are converted into numbers, using their initial positions. There are detailed examples in the PHP manual.

Arrays and hash tables

Arrays are supported in the same way as hash tables. How you use them depends on how you define them. You can define them as list() or array(), or you can assign values directly to arrays. The index of an array starts at 0. Although I haven't explained it here, you can use multidimensional arrays just as easily.

//an array of two elements $a[0] = "first"; $a[1] = "second"; $a[] = "third"; //Simple way to add array elements //$a[2] is now assigned "third" echo count($a); //prints 3 because the array has 3 elements //define an array with a statement and assign values $myphonebook = array ( "sbabu" => "5348", "keith" => "4829", "carole" => "4533" ); //Oh, forget the Dean, let's add an element $myphonebook["dean"] = "5397"; //you defined carale element incorrectly, let's correct it $myphonebook["carole"] => "4522" //Haven't I told you how to use arrays in a similar way? let's take a look at echo "$myphonebook[0]"; // sbabu echo "$myphonebook[1]"; // 5348

Other useful functions for arrays or hash tables include sort(), next(), prev(), and each().

object

Use the new statement to generate an object:

class foo { function do_foo () { echo "Doing foo. "; } } $bar = new foo; $bar->do_foo();

Change variable type

In the PHP manual it is mentioned that "PHP does not support (and does not require) defining variable types directly when declaring variables; variable types will be determined by the circumstances in which they are applied. If you assign var a string, it becomes a string. If you assign it an integer value, it becomes an integer. "

$foo = "0"; // $foo is a string (ASCII 48) $foo++; // $foo is the string "1" (ASCII 49) $foo += 1; // $foo is now an integer (2) $foo = $foo + 1.3; // $foo is a double (3.3) $foo = 5 + "10 Little Piggies"; // $foo is an integer (15) $foo = 5 + "10 Small Pigs"; // $foo is an integer (15)

If you want to forcibly convert a variable type, you can use the same function settype() as in C.

V. Variables and constants

As you may have noticed, variables are prefixed with a dollar sign ($). All variables are local, so to make it possible to use external variables in a defined function, use the global statement. If you want to restrict the scope of the variable to the function, use the static statement.

$g_var = 1 ; //global scope function test() { global $g_var; //This allows you to declare global variables }

More advanced is the variable representation of variables. Please refer to PHP manual. This can sometimes seem useful.

PHP has many built-in variables. You can also define your own constants with define functions, such as define("CONSTANT","value").

VI. Operators

PHP has operators commonly found in C,C++, and Java. The precedence of these operators is also consistent. Assignment also uses "=".

Arithmetic and Character

Only one of the following operators is character-specific:

$a + $b: plus

$a - $b: minus

$a * $b: Multiply

$a / $b: except

$a % $b: modulo (remainder)

$a . $b: String concatenation

logical sum comparison

The logical operators are:

$a ||$b: or

$a or $b: or

$a && $b: with

$a and $b: and

$a xor $b: XOR (true when $a or $b is true, false when both are the same)

! $a: not

Comparison operators are:

$a == $b = equal

$a != $b: unequal

$a

< $b :小于 $a $b :大于 $a >

= $b :大于等于

与C一样PHP也有三重运算符(?:)。位操作符在PHP同样存在。

优先权

就和C以及Java一样!

七、 控制流程结构

PHP有着与C一样的流程控制。我将在下面大概介绍。

if, else, elseif, if(): endif

if (表达式一) { . . . } elseif (表达式二) { . . . } else { . . . } // 或者像Python一样 if (表达式一) : . . . . . . elseif (表达式二) : . . . else : . . . endif ;

Loops. while, do..while, for

while (表达式) { . . . } do { . . . } while (表达式); for (表达式一; 表达式二; 表达式三) { . . . } //或者像Python一样 while (expr) : . . . endwhile ;

switch

switch是对多重if-elseif-else结构的***的替换:

switch ($i) { case 0: print "i equals 0"; case 1: print "i equals 1"; case 2: print "i equals 2"; }

break, continue

break中断当前的循环控制结构。

continue被用来跳出剩下的当前循环并继续执行下一次循环。

require, include

就像C中的#include预处理一样。你在require中指定的那个文件将替代其在主文件中的位置。在有条件的引用文件时,可以使用include()。这样就使得你可以将复杂的PHP文件分割成多个文件并且在不同需要时分别引用它们。

八、 函数

你可以像以下的例子一样定义自己的函数。函数的返回值可以是任何数据类型:

function foo (变量名一, 变量名二, . . . , 变量名n) { echo "Example function.\n"; return $retval; }

所有PHP代码都可以出现在函数定义中,甚至包括对其他函数和类的定义。函数必须在引用之前定义。

九、 类

利用类模型建立类。可以参考PHP手册中对类的详细解释。

class Employee { var $empno; // 员工人数 var $empnm; // 员工姓名 function add_employee($in_num, $in_name) { $this->empno = $in_num; $this->empnm = $in_name; } function show() { echo "$this->empno, $this->empnm"; return; } function changenm($in_name) { $this->empnm = $in_name; } } $sbabu = new Employee; $sbabu->add_employee(10,"sbabu"); $sbabu->changenm("babu"); $sbabu->show();以上是"PHP入门知识点有哪些"这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注行业资讯频道!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report