google+javascriptbanktwitter@js_bankfacebook@jsbankrss@jsbank






JavaScript và PHP - So sánh để học hỏi Bài viết này liệt kê vài sự giống và khác nhau giữa hai ngôn ngữ lập trình web rất phổ biến trong thế giới mã nguồn mở: JavaScript và PHP. Các so sánh trong bài viết này nói về biến, đối tượng và mảng, các cấu trúc điều khiển của hai ngôn ngữ này; để từ đó các lập trình viên có cái nhìn chính xác hơn về chúng, và đưa ra các giải pháp xử lý phù hợp với yêu cầu công việc đặt ra.


Miễn phí web hosting 1 năm đầu tại iPage



Nếu bạn vẫn còn đang tìm kiếm một nhà cung cấp hosting đáng tin cậy, tại sao không dành chút thời gian để thử với iPage, chỉ với không quá 40.000 VNĐ/tháng, nhưng bạn sẽ được khuyến mãi kèm với quà tặng trị giá trên 10.000.0000 VNĐ nếu thanh toán cho 24 tháng ~ 900.000 VNĐ?

Có trên 1 triệu khách hàng hiện tại của iPage đã & đang hài lòng với dịch vụ, tuyệt đối chắc chắn bạn cũng sẽ hài lòng giống họ! Quan trọng hơn, khi đăng ký sử dụng web hosting tại iPage thông qua sự giới thiệu của chúng tôi, bạn sẽ được hoàn trả lại toàn bộ số tiền bạn đã sử dụng để mua web hosting tại iPage. Wow, thật tuyệt vời! Bạn không phải tốn bất kì chi phí nào mà vẫn có thể sử dụng miễn phí web hosting chất lượng cao tại iPage trong 12 tháng đầu tiên. Chỉ cần nói chúng tôi biết tài khoản của bạn sau khi đăng ký.

Nếu muốn tìm hiểu thêm về ưu / nhược điểm của iPage, bạn hãy đọc đánh giá của ChọnHostViệt.com nhé!
Thử iPage miễn phí cho năm đầu tiên NGAY

This is a basic comparison between PHP and JavaScript. It's intended for users familiar with PHP and looking for JavaScript equivalents.

JavaScript and PHP Comparisons:

Variables

Variable Scope

PHP and JavaScript take two very different approaches to declaring variables. In PHP, all variables are local in scope unless declared as global. JavaScript is opposite, and all variables are global unless declared with the var keyword.

PHP

<?php
function foo() {
 
$variable_a = 'value'; // Local variable declaration.
}
function
bar() {
  print
$variable_a; // Prints nothing.
}

function
foo() {
  global
$variable_b; // Global variable declaration.
 
$variable_b = 'value';
}
function
bar() {
  global
$variable_b;
  print
$variable_b; // Prints 'value'.
}
?>

JavaScript

function foo() {
  var variableA = 'value'; // Local variable with use of "var".
}
function bar() {
  alert(variableA); // Variable not defined error.
}

function foo() {
  variableB = 'value'; // Global variable, no "var" declaration.
}
function bar() {
  alert(variableB); // alert('value')
}

An interesting twist is JavaScript also allows scoping within functions. When using the "var" declaration, variables are available for everything in the current function or any sub-functions.

PHP

function foo() {
  $variable_a = 'value'; // Local variable declaration.
  function bar() {
    print $variable_a; // Prints nothing.
  }
}

JavaScript

function foo() {
  var variableA = 'value'; // Local variable with use of "var".
  function bar() {
    alert(variableA); // alert('value');
  }
}

Variable Types

Both PHP and JavaScript are loosely typed, meaning a variable can be of any type, and change from one type to another. However both PHP and JavaScript keep track of the type of variables, and you can check this type.

PHP

<?php
$foo
= 3;
is_int($foo); // TRUE

$foo = '3';
is_int($foo); // FALSE
is_string($foo); // TRUE
?>

JavaScript

var foo = 3;
type_of(foo); // 'number'

foo = '3';
type_of(foo); // 'string'

Casting Variables

Every now and then you might need to cast variables to a specific type. This is extremely important when dealing with JavaScript's + operator, which is used for both string concatenation and for numeric addition.

PHP
In PHP, variables may be cast to certain type by using parenthesis. String concatenation is done with "." and addition with "+".

<?php
$foo
= '3.5 kg';
$bar = (float)$foo; // 3.5
$bar = (int)$foo; // 3
$baz = (string)$foo; // '3.5 kg'

print $bar + $baz; // 6
print $bar . $baz; // '33'
?>

JavaScript
JavaScript has functions specifically for casting variables to numbers. Both string concatenation and addition is done with "+". If mixing a string and a number with "+", concatenation will take precedence over addition.

var foo = '3.5 kg';
var bar = parseFloat(foo); // 3.5
    bar = parseInt(foo); // 3
var baz = '3';

alert(bar + baz); // '33'
alert(bar + parseInt(baz)); // 6

Checking for NULL or empty() values

Variables in PHP don't have to be defined for you to use them, though if you're working with E_ALL compliance on (not the default of most PHP installs), your script will throw a notice if you try to use an undeclared variable. JavaScript is a bit mixed concerning undeclared variables, if you attempt to modify or compare with an undeclared variable, the script will break entirely, but you can check the variable status using typeof() or in conditional statements containing only that variable.

PHP

<?php
// Check if a variable is declared at all.
if (!isset($foo)) {
 
$foo = TRUE;
}

// Or check if a variable has a value that equates to FALSE.
// This includes variables that have not been declared.
if (empty($bar)) {
 
$bar = TRUE;
}
?>

JavaScript

// Check if a variable is declared at all.
if (typeof(foo) == 'undefined') {
  var foo = true;
}

// Or check if a variable has a value that equates to false.
// This includes variables that have not been declared.
if (!bar) {
  var bar = true;
}

// However an undeclared variable can't be used in comparisons.
if (baz == false) { // Variable undefined error.
  var baz = true;
}

Boolean Variables

A simple but important thing to remember is that JavaScript only recognizes the keyword true in all lowercase. PHP accepts both uppercase and lowercase.

PHP

<?php
is_boolean
(TRUE); // TRUE
is_boolean(true); // TRUE
is_boolean(True); // TRUE
?>

JavaScript

typeof(true); // 'boolean'
typeof(TRUE); // 'undefined'
typeof(True); // 'undefined'

Case Sensitivity

Both JavaScript and PHP are case sensitive in their variables. PHP is not case-sensitive in function or class declarations, but JavaScript is case sensitive for these also.

PHP

<?php
// Variable case:
$foo = 'bar';
print
$foo; // Prints 'bar'.
print $Foo; // Prints nothing.

// Function case:
function foo() {
  print
'bar';
}
foo(); // Prints 'bar'.
Foo(); // Prints 'bar'.
?>

JavaScript

// Variable case:
var foo = 'bar';
alert(foo); // alert('bar')
alert(Foo); // Variable not defined error.

// Function case:
function foo() {
  alert('bar');
}
foo(); // alert('bar')
Foo(); // Function not defined error.
iPhoneKer.com
Save up to 630$ when buy new iPhone 15

GateIO.gomymobi.com
Free Airdrops to Claim, Share Up to $150,000 per Project

https://tooly.win
Open tool hub for free to use by any one for every one with hundreds of tools

chatGPTaz.com, chatGPT4.win, chatGPT2.fun, re-chatGPT.com
Talk to ChatGPT by your mother language

Dall-E-OpenAI.com
Generate creative images automatically with AI

AIVideo-App.com
Render creative video automatically with AI

JavaScript theo ngày


Google Safe Browsing McAfee SiteAdvisor Norton SafeWeb Dr.Web