PHP、JavaScript 语法对照、速查
全栈工程师看过来,学的计算机语言多了,往往会把不同语言的各个函数搞混。作为一个全栈PHPer,往往PHP、JavaScript 语法傻傻分不清楚,百度一下,查手册要网速。不如收藏下这篇文章,打印出来,贴到一旁快速查阅。
相关推荐:《PHP视频教程》《javascript高级教程》
编码风格JavaScript 的一些数组map函数有jQuery实现,ES6后,又出了官方实现。PHP 的数组、字符串相关函数的命名随性,这仨一块就更容易混淆了。
;
号是必须的,\n
不是必须的换行 \n
,以及 ;
号都不是必须的,for(;;)除外define(‘VAR_NAME’, 12);const MY_FAV = 7; (ES6引入的标准)
var varName = 3;
if (true) {
let varName2 = 2;
}
}
(函数作用域内必须用var声明,否则变量全局可访问.)
(let修饰的变量就是块级别作用域,ES6引入)
function myFunc() {
global $varName;
}
(函数内使用全局变量,必须要用global变量声明使用外部的全局变量)var varName1 = 3;
varName2 = 2;
function myFunc() {
varName3 = 6;
} (这里写法varName1,2,3都是全局变量)
global 对象(nodejs环境)
$bar = (bool) $foo;
$bar = boolval($foo);boolVal = Boolean(‘’)
$bar = (integer) $foo;
$bar = intval($foo);intVal = Number(“314”)
intVal = parseInt(“3.14”)
$bar = (double) $foo;
$bar = (real) $foo;
$bar = floatval($foo);floatVal = Number(“3.14”)
flotaVal = parseFloat(“12”)
$bar = strval($foo);str = String(123)
str = (123).toString()
let obj = {…arr}
$date->setTimestamp(1171502725);var date = new Date(1398250549490);
new Date().constructor === Date
$a = $a ? : 1;//第二种 PHP5.3支持re = isMember ? 2.0 : ‘$10.00’
$array = [ “foo” => “bar”, “bar” => “foo”]; // PHP 7语法b = [1,2,3]
$arr[key1] = value1;
$arr[key2] = value2;var mycars=new Array()
mycars[0]=”Saab”
mycars[1]=”Volvo”
mycars[2]=”BMW”
var mycars = new Array(“Saab”,”Volvo”,”BMW”)
{
echo $i ;
}
for (var i=0; i < cars.length ; i++)
{
document.write(cars[i]);
}
foreach ($x as $value) {
echo $value;
}var person= {fname:”John”,lname:”Doe”,age:25};
for (x in person) {
txt=txt + person[x];
}
echo $i ;
$i++;
}while (i<5) {
x=x + “num is “ + i ;
i++;
}
$i++;
echo $i;
} while ($ i<= 5);do {
console.log(i);
i++;
} while (i < 5);
{
echo $n;
}
$b = array(“uno”, “dos”, “tres”, “cuatro”, “cinco”);
$c = array_map(“show_Spanish”, $a);$.each([ 52, 97 ], function( index, value ) {
alert( index + “: “ + value );
});
// ↑ 这是 jQuery 方式
const items = [‘item1’, ‘item2’, ‘item3’];
items.forEach(function(item, index, arr){
console.log(‘key:’ + index + ‘ value:’ + item);
});
(ES6引入)
$carry += $item;
return $carry;
}
$a = array(1, 2, 3, 4, 5);
var_dump(array_reduce($a, “sum”)); // int(15)var numbers = [65, 44, 12, 4];
function getSum(total, num) {
return total + num;
}
console.log(numbers.reduce(getSum));
始于ECMAScript 3
// returns whether the input integer is odd
return($var & 1);
}
$array1 = array(“a”=>1, “b”=>2, “c”=>3, “d”=>4, “e”=>5);
echo “Odd :\n”;
array_filter($array1, “odd”);function isBigEnough(element) {
return element >= 10;
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough); \\ JavaScript 1.6 引入
\\比较特殊的是PHP在双引号字符中可以解析变量
$str2 = ‘tow string’;var carname = “Volvo XC60”;
var carname = ‘Volvo XC60’;
(同样的在双引号中可以使用转义字符)
foo
bar
EOT;
var tmpl =’\
!!! 5\
html\
include header\
body\
include script’
str.slice(1,5);
$pieces = explode(“ “, $pizza);
echo $pieces[0]; // piece1var str=”How are you doing today?”;
var n=str.split(“ “);
\ output:How,are,you,doing,today?
(PHP 函数的可定制要强一点)var str = “ string “;
alert(str.trim());
$pos = strpos($mystring, ‘cs’);var str=”Hello world, welcome to the universe.”;
var n=str.indexOf(“welcome”);
person={firstname:”John”,lastname:”Doe”,age:50,eyecolor:”blue”};
$obj->a = 12;var myCar = new Object();
myCar.year = 1969; // js还可以以数组形式
myCar[“year”] = 1969;
delete object[‘property’]
var myRe = new RegExp(“d(b+)d”, “g”);
use Package as Package1, Package2;const http = require(‘http’) (CommonJS规范)
import “my-module”;
import {foo as fo, bar} from “my-module”;
(es6实现,import需要和export配合使用)
require ‘bc.php’;
<script type='text/javascript' src='b.js'></script>
(仅在html中用)foreach ($numbers as $n) {
$sum += $n;
}
}
echo add(1, 2, 3, 4); // PHP5.6 开始支持function myFunction(x, y, z) { }
var args = [0, 1, 2];
myFunction(…args); (ES6开始支持)
list($a, $b, $c) = $my_array;
// php5, 如果是php7版本支持以下语法
[‘a’=>$a, ‘c’=>$c] = $my_array;var date1 = [1970, 2, 1];
[ year, mouth ]= date1;
var date2 = {year: 1980, mouth: 3, day: 21};
({ mouth } = date2);
console.log(date1);
console.log(year);
console.log(mouth);
欢迎大家收藏,如果你觉得需要补充的地方,请留言。
以上就是全栈工程师看过来!PHP Javascript语法对照、速查的详细内容,
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)