


#Qp, JavaScript gram -comparison, speed check
all to see that there are more computer languages to learn, often mixing various functions of different languages. As a full-stack PHPer, the syntax of PHP and JavaScript is often unclear. I need to search on Baidu and check the manual to find the Internet speed. Why not bookmark this article, print it out, and put it aside for quick reference.
Related recommendations: "PHP Video Tutorial" "javascript Advanced Tutorial"
Some array map functions of JavaScript are implemented by jQuery. After ES6, an official implementation was released. PHP's array and string related functions are named randomly, making it easier to confuse these three.
Coding style
Language | PHP | JavaScript |
---|---|---|
; is required, \n is not required
| Newline\n, and ; are not required, except for(;;)
|
|
Only variable names are case-sensitive | Variable names, function names, class names, etc. are all case-sensitive | |
declare( strict_types=1); (new feature of PHP7) | "use strict";(introduced in ECMAScript 5) |
Variable declaration
PHP | JavaScript | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
const VAR_NAME = 12; | define('VAR_NAME', 12); | const MY_FAV = 7; (standard introduced in ES6)|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$varName = 12; (PHP only has function scope and global scope) | function myFunc() { | var varName = 3; if (true) { let varName2 = 2; } } (Var must be declared within the function scope, otherwise the variable is globally accessible.) (The variable modified by let is block level Scope, introduced in ES6) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$varName = 12; | function myFunc() { global $varName; } (To use global variables within a function, you must use global variables to declare external global variables) | var varName1 = 3; varName2 = 2; function myFunc() { varName3 = 6; } (Writing here varName1, 2, and 3 are all global variables) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$GLOBALS array | window object (html environment) | global object (nodejs environment) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
null | undefined |
Language | PHP | JavaScript |
---|---|---|
Convert to bool, boolean | $bar = (boolean) $foo; $bar = (bool) $foo; $bar = boolval($foo ); |
boolVal = Boolean('') |
Convert to int | $bar = (int) $foo; $bar = (integer) $foo; $bar = intval($foo); |
intVal = Number(“314”) intVal = parseInt(“3.14”) |
Convert to float | $bar = (float) $foo; $bar = (double) $foo; $bar = (real) $foo; $ bar = floatval($foo); |
floatVal = Number(“3.14”) floataVal = parseFloat(“12”) |
$bar = (string) $foo; | $bar = strval($foo); | str = String(123) str = (123).toString() |
$arr = (array) new stdClass(); | (requires multi-line function to complete) | |
$obj = (object) array('1' => 'foo'); | let arr = ['yellow', 'white ', 'black']; | let obj = {...arr} |
$date = new DateTime(); | $date->setTimestamp(1171502725); | var date = new Date(1398250549490);|
$dateObj = new DateTime($dateStr); | var myDateObj = new Date(Date.parse(datetimeStr)) | |
(unset) $ var; \ does not delete the variable or unset its value. Just return NULL value | ||
$varType = gettype($var); | varType = typeof myCar | |
$boolRe = $a instanceof MyClass; | boolRe = a instanceof MyClass | new Date() .constructor === Date |
Magic Variable
PHP | JavaScript | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
$filePath = __FILE__; | filePath = __filename | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
$currentDir = __DIR__; | curDir = __dirname | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
__LINE__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
__FUNCTION__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
__CLASS__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
__NAMESPACE__ |
Language | PHP | JavaScript |
---|---|---|
Ternary (ternary) operation | $a = $a ? $a : 1;//The first type $a = $a ? : 1; //The second type of PHP5.3 supports |
re = isMember ? 2.0 : '$10.00' |
merge operator | $a = $a ?? 1; // PHP7 supports |
##array
PHP | JavaScript | |
---|---|---|
$a=array(0 => 1, 1 => 2,4,5,6); | $array = [ “foo” => “bar”, “bar” => “foo”]; // PHP 7 Syntax
b = [1,2,3] |
|
$arr = array(); | $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") |
language
JavaScript | ##for loop | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
} |
for (var i=0; i { document.write (cars[i]); } |
##foreach, for in loop $x=array(“one”,”two”,”three” ); | foreach ($x as $value) { ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
var person= {fname:”John”,lname:”Doe”,age:25 }; | for (x in person) { txt=txt person[x]; } |
##while loop while ($ i echo $i ; | $i ; ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
while (ix=x “num is “ i ; | i ; } | ##do while loop
do { $i ; echo $i; | } while ( $ i||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
console.log(i); | i ;} while (i |
Language | PHP | JavaScript |
---|---|---|
Get the number of elements in the array | count($arr); | arrayObject.length |
array_merge($arr1, $arr2); | arr1.concat(arr2) | |
unset($arr[$key]); | delete arr1[key] | |
implode(', ', $arr1); | arr.join(',') | |
$re = array_pop($ arr1); | re = arrayObject.pop() | |
array_push($arr1, $var1); | len = arrayObject.push(newele1) | |
$re = array_shift($arr1) ; | re = arrayObject.shift() | |
array_unshift($arr1, $var1) ; | len = arrayObject.unshift(newele1) | |
$newArr = array_splice($ arr1,$start,$len); | newArr = arrayObject.slice(start,end) | |
sort($arr1); | arrayObject.sort(sortByFunc = null) | |
array_reverse(&$arr, $keepKeys = true); | arrayObject.reverse() | |
function map_Spanish($n) | { echo $n; } $b = array(“uno”, “dos” , "tres", "cuatro", "cinco"); $c = array_map("show_Spanish", $a); $.each([ 52, 97 ], function( index, value ) { | alert( index “: “ value );}); // ↑ This is the jQuery way const items = ['item1', 'item2', 'item3'] ; items.forEach(function(item, index, arr){ console.log('key:' index ' value:' item); }); (Introduced in ES6) |
function sum($carry, $item) { | $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)); Started from ECMAScript 3 |
function odd($var) { | // 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 introduced |
characters
Language | PHP | JavaScript |
---|---|---|
Create | $str = "a string"; \\What's special is that PHP can parse variables in double quote characters $str2 = 'tow string'; |
var carname = "Volvo XC60"; var carname = 'Volvo XC60'; (Similarly, escape characters can be used in double quotes) |
Multi-line characters | $bar = foo bar EOT; |
var tmpl ='\ !!! 5\ html \ include header\ body\ include script' |
$str1 . $str2 | str1 str2 |
##String function
JavaScript | ||
---|---|---|
string.length | Get substring | |
string.substr(start,length ) | str.slice(1,5); | |
$pieces = explode(“ “, $pizza); | echo $pieces[0]; // piece1
var str=”How are you doing today?”; var n=str.split(“ “); | \ output:How,are,you,doing,today?
| Remove whitespace characters at the beginning and end of the string (or Other characters)
(PHP functions are more customizable) | var str = “ string “; alert(str.trim()); |
|
$pos = strpos($mystring, 'cs'); | var str="Hello world, welcome to the universe."; var n=str.indexOf ("welcome"); |
|
string.toLowerCase() | Convert the string to uppercase | |
string.toUpperCase() |
Language
JavaScript | |||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
var argv = arguments \\ Direct object within a function |
Object
##Regular
Mathematical functions
Language
|
The above is the detailed content of Full stack engineers come here! PHP Javascript syntax comparison and quick check. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
