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

Objects and Arrays

In PHP, objects and arrays are two distinctly different things and have different syntaxes. In JavaScript, objects and arrays are often interchangeable, and you can switch between syntaxes freely.

Declaring an Object or Array

There are a few different ways to declare an object or an array in both JavaScript and PHP. The key difference between PHP and JavaScript is that JavaScript does not have associative arrays. Arrays in JavaScript are always numeric based. However, since objects may use array-like syntax, simply declare a new object when you'd use an associative array in PHP.

PHP

<?php
// Define an array.
$foo = array(); // New empty array.
$foo = array('a', 'b', 'c'); // Numeric index.
$foo = array('a' => '1', 'a' => '2', 'c' => '3'); // Associative.

// Define an object.
$bar = new stdClass(); // New empty object.
$bar->a = '1';
$bar->b = '2';
$bar->c = '3';
?>

JavaScript

// Define an array (longhand).
var foo = new Array(); // New empty array.
var foo = new Array('a', 'b', 'c'); // Numeric index.

// Define an array (shorthand, more common).
var foo = []; // New empty array.
var foo = ['a', 'b', 'c']; // Numeric index.

// Define an object.
var bar = {}; // New empty object.
var bar = {   // New populated object.
  a: '1',
  b: '2',
  c: '3'
};

As you might notice in the last example, declaring an object in JavaScript uses the format commonly known as JSON, which stands for "JavaScript Object Notation". JSON strings having become very popular as a faster alternative to XML, and can be read and created with the PHP functions json_encode() and json_decode().

Object and Array Syntax

JavaScript and PHP are very similar in array notation, though they differ more in their object notation. The key difference is that PHP uses an array "->" to reference items within objects, while JavaScript uses the dot ".".

PHP

<?php
$foo
= array('a', 'b', 'c'); // New numeric index array.
print $foo[0]; // 'a'

$bar = new stdClass();
$bar->a = '1';
print
$bar->a; // '1';
?>

JavaScript

var foo = ['a', 'b', 'c']; // New array.
alert(foo[0]); // 'a'

var bar = { a: '1', b: '2', c: '3' }; // New object.
alert(bar.a); // '1'

Using Objects as Associative Arrays

Let's take one more look at defining an object in JavaScript and see how it can be used to compensate for the lack of associative arrays in JavaScript.

PHP

<?php
$bar
= array(
 
'a' => '1',
 
'b' => '2',
 
'c' => '3',
);
?>

JavaScript
JavaScript doesn't have associative arrays, but defining an object works identically to an associative array in PHP.

var bar = {
  a: '1',
  b: '2',
  c: '3'
};

As mentioned earlier, in JavaScript array and object syntaxes can be mixed freely, so this new object can be referenced either as an array or an object.

// Using array syntax, even though this is an object.
alert(bar['a']); // '1'

// Or the standard object property syntax.
alert(bar.b);    // '2'

If we had a multi-level object, we can even combine the bracket and dot syntaxes.

var bar = {
  a: { red: 'my favorite', blue: 'not so bad' },
  b: '2',
  c: '3'
}

alert(bar.a['red']); // 'my favorite'
alert(bar['a'].blue); // 'not so bad'

Dumping variables

PHP

<?php
var_dump
($foo);
// Or
print_r($foo);
?>

JavaScript

console.log(foo); // Prints to Firebug or Safari console.

Logic Constructs

for()

The classic for() construct is supported nearly identically in PHP and JavaScript.

PHP

<?php
for ($n = 0; $n < 10; $n++) {
  print
$n;
}
?>

JavaScript

// Note that variables should always be
// prefixed with "var" to define a local scope.
for (var n = 0; n < 10; n++) {
  alert(n);
}

foreach()

PHP's foreach() construct can easily be converted to JavaScript's for().

PHP

<?php
foreach ($array as $key => $value) {
 
// Do something.
}
?>

JavaScript

for (var key in array) {
  // There is no "value" directly, but you can get it easily.
  var value = array[key];
  // Do something.
}

Wrap up
There are still a lot of other topics that could be covered (JavaScript is a language with a lot of tricks), but this should be a good foundation to work from. Hope you enjoy!

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