一门语言的骨架是什么?

2018-06-28  本文已影响54人  张俊凯

在文章《如何掌握所有的程序语言》中,王垠指出,任何语言都是语言特性的组合,常见的语言特性有

  • 变量定义
  • 算数运算
  • for 和while循环语句
  • 函数定义,函数调用
  • 递归
  • 静态类型系统
  • 类型推导
  • lambda函数
  • 面向对象
  • 垃圾回收
  • 指针算术
  • goto 语句
    在掌握了特性后,马上开始着手解决一个具体的问题,让概念逐渐内化,那么,PHP的特性都是如何具体表示的呢?

php中变量支持整数integer,浮点数float,字符串String,数组array,开头用$符号表示一个变量,根据变量内容自动推断类型,只能用字母和下划线开头,数字开头非法,并且区分大小写。

<html>

<head></head>

<body>

Agent: So who do you think you are, anyhow?

<br />

<?php

// define variables

$name = 'Neo';

$rank = 'Anomaly';

$serialNumber = 1;

// print output

echo "Neo: I am <b>$name</b>, the <b>$rank</b>. You can call me by my serial number, <b>$serialNumber</b>.";

?>

赋值用 = 号

<?php

$age = $dob + 15;

?>

支持的类型有:
布尔值

<?php

$auth = true;

?>

整数

<?php

$age = 99;

?>

小数

<?php

$temperature = 56.89;

?>

字符串
字符串可以用单引号和双引号包起来,当用双引号的时候 ,字符串里的特殊字符会被解析

<?php

$identity = 'James Bond';

$car = 'BMW';

// this would contain the string "James Bond drives a BMW"

$sentence = "$identity drives a $car";

echo $sentence;

?>

====================================分割线=============================

<html>

<head>

</head>

<body>

<?php

// set quantity

$quantity = 1000;

// set original and current unit price

$origPrice = 100;

$currPrice = 25;

// calculate difference in price

$diffPrice = $currPrice - $origPrice;

// calculate percentage change in price

$diffPricePercent = (($currPrice - $origPrice) * 100)/$origPrice

?>



<table border="1" cellpadding="5" cellspacing="0">

<tr>

<td>Quantity</td>

<td>Cost price</td>

<td>Current price</td>

<td>Absolute change in price</td>

<td>Percent change in price</td>

</tr>

<tr>

<td><?php echo $quantity ?></td>

<td><?php echo $origPrice ?></td>

<td><?php echo $currPrice ?></td>

<td><?php echo $diffPrice ?></td>

<td><?php echo $diffPricePercent ?>%</td>

</tr>

</table>

</body>

</html>

高级运算

<?php

// this...

$a = 5;

$a = $a + 10;

// ... is the same as this

$a = 5;

$a += 10;

?>

拼接字符串

<?php

// set up some string variables

$a = 'the';

$b = 'games';

$c = 'begin';

$d = 'now';

// combine them using the concatenation operator

// this returns 'the games begin now<br />'

$statement = $a.' '.$b.' '.$c.' '.$d.'<br />';

print $statement;

// and this returns 'begin the games now!'

$command = $c.' '.$a.' '.$b.' '.$d.'!';

print $command;

?>

高级拼接字符串

<?php

// define string

$str = 'the';

// add and assign

$str .= 'n';

// str now contains "then"

echo $str;

?>

参考链接:https://devzone.zend.com/4/php-101-part-1-down-the-rabbit-hole/

上一篇 下一篇

猜你喜欢

热点阅读