返回介绍

PHP lexical structure

发布于 2025-02-22 22:19:59 字数 7940 浏览 0 评论 0 收藏 0

Computer languages, like human languages, have a lexical structure. A source code of a PHP script consists of tokens. Tokens are atomic code elements. In PHP language, we have comments, variables, literals, operators, delimiters, and keywords.

Comments

Comments are used by humans to clarify the source code. All comments in PHP follow the # character.

<?php

# comments.php
# Author Jan Bodnar
# ZetCode 2016

echo "This is comments.php script\n";

?>

Everything that follows the # character is ignored by the PHP interpreter.

// comments.php
// author Jan Bodnar
// ZetCode 2016

/*
 comments.php
 author Jan Bodnar
 ZetCode 2016
*/

PHP also recognizes the comments from the C language.

White space

White space in PHP is used to separate tokens in PHP source file. It is used to improve the readability of the source code.

public $isRunning;

White spaces are required in some places; for example between the access specifier and the variable name. In other places, it is forbidden. It cannot be present in variable identifiers.

$a=1;
$b = 2;
$c  =  3;

The amount of space put between tokens is irrelevant for the PHP interpreter. It is based on the preferences and the style of a programmer.

$a = 1;
$b = 2; $c = 3;
$d 
  = 
   4;

We can put two statements into one line. Or one statement into three lines. However, source code should be readable for humans. There are accepted standards of how to lay out your source code.

Semicolon

A semicolon is used to mark the end of a statement in PHP. It is mandatory.

$a = 34;
$b = $a * 34 - 34;
echo $a;

Here we have three different PHP statements. The first is an assignment. It puts a value into the $a variable. The second one is an expression. The expression is evaluated and the output is given to the $b variable. The third one is a command. It prints the $a variable.

Variables

A variable is an identifier, which holds a value. In programming we say that we assign a value to a variable. Technically speaking, a variable is a reference to a computer memory, where the value is stored. In PHP language, a variable can hold a string, a number, or various objects like a function or a class. Variables can be assigned different values over time.

Variables in PHP consist of the $ character, called a sigil, and a label. A label can be created from alphanumeric characters and an underscore _ character. A variable cannot begin with a number. The PHP interpreter can then distinguish between a number and a variable more easily.

$Value
$value2
$company_name

These were valid PHP identifiers.

$12Val
$exx$
$first-name

These were examples of invalid PHP identifiers.

The variables are case sensitive. This means that $Price , $price , and $PRICE are three different identifiers.

case.php

<?php

$number = 10;
$Number = 11;
$NUMBER = 12;

echo $number, $Number, $NUMBER;

echo "\n";

?>

In our script, we assign three numeric values to three variables and print them. However, for clarity reasons, it is not recommended to create variables which differ only in case; it is considered a poor practice.

$ php case.php 
101112

This is the output of the script.

Constants

A constant is an identifier for a value which cannot change during the execution of the script. By convention, constant identifiers are always uppercase.

constants.php

<?php

define("SIZE", 300); 
define("EDGE", 100); 

#SIZE = 100;

echo SIZE;
echo EDGE;

echo "\n";

?>

In the script, we define two constants.

define("SIZE", 300); 
define("EDGE", 100);

Constants are created with the define() function.

#SIZE = 100;

Constants differ from variables; we cannot assign a different value to an existing constant. The script will fail if we uncomment the line.

echo SIZE;
echo EDGE;

Constants do not use the dollar sigil character.

$ php constants.php 
300100

This is the output of the constants script.

The following is a list of PHP compile time constants.

__CLASS__  __DIR__     __FILE__  __FUNCTION__ 
__METHOD__   __NAMESPACE__ 

A literal

A literal is any notation for representing a value within the PHP source code. Technically, a literal is assigned a value at compile time, while a variable is assigned at runtime.

$age = 29;
$nationality = "Hungarian";

Here we assign two literals to variables. Number 29 and string "Hungarian" are literals.

literals.php

<?php

$name1 = "Jane ";
$age1 = 17;

$name2 = "Rose ";
$age2 = 16;

echo "Patrick 34\n";
echo "Luke 22\n";

echo $name1, $age1, "\n";
echo $name2, $age2, "\n";

?>

If we do not assign a literal to a variable, there is no way how we can work with it—it is dropped.

$ php literals.php
Patrick 34
Luke 22
Jane 17
Rose 16

This is the output of the literals.php script.

Operators

An operator is a symbol used to perform an action on some value.

+  -  *  /  %  ++  --
=  +=   -=   *=   /=   .=  %=
==   !=   ><   >   <   >=   <= 
&&   ||   !   xor   or  
&  ^  |   ~   .   <<   >>   

These are PHP operators. We will talk about operators later in the tutorial.

Delimiters

A delimiter is a sequence of one or more characters used to specify the boundary between separate, independent regions in plain text or other data stream.

$a = "PHP";
$b = 'Java';

The single and double characters are used to mark the beginning and the end of a string.

function setDate($date) {
  $this->date = $data;
}

if ( $a > $b) {
  echo "\$a is bigger than \$b";
}

Parentheses are used to mark the function signature. The signature is the function parameters. Curly brackets are used to mark the beginning and the end of the function body. They are also used in flow control.

$a = array(1, 2, 3);
echo $a[1];

The square brackets are used to mark the array index.

/*
 Author Jan Bodnar
 January 2016
 ZetCode
*/

/* */ delimiters are used to provide C style comments in PHP.

<?php
// PHP code
?>

The <?php and ?> delimiters are used to delimit PHP code in a file.

Keywords

A keyword is a reserved word in the PHP programming language. Keywords are used to perform a specific task in a computer program; for example, print a value, do repetitive tasks, or perform logical operations. A programmer cannot use a keyword as an ordinary variable.

The following is a list of PHP keywords.

abstract  and      array()   as      break
case    catch    class     clone     const  
continue  declare    default   do      else
elseif    enddeclare   endfor    endforeach  endif
endswitch   endwhile   extends   final     for 
foreach   function   global    goto    if  
implements  interface  instanceof  namespace   new   
or      private    protected   public    static
switch    throw    try     use     var 
while     xor

Next we have other language constructs.

die()       echo()      empty()   exit()    eval()
include()     include_once()  isset()   list()    require()
require_once()  return()    print()   unset()   

In this part of the PHP tutorial, we covered the basic lexis for the PHP language.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文