• About

Viet PHP

PHP = ['newbie' => 'cơ bản', 'coder' => 'nâng cao', 'professional'=>'chuyên sâu']

  • PHP
    • OOP
    • Basic
  • WordPress
  • Bài tập PHP
  • PHP 7
Home PHP Ép kiểu trong PHP và toán tử so sánh tương đương và tương đồng PHP

Ép kiểu trong PHP và toán tử so sánh tương đương và tương đồng PHP

Posted on April 13, 2018 Written by admin Leave a Comment

Các bác PHP luôn phải thừa nhận về tính lỏng lẻo của việc khai báo dữ liệu cho biến. Nhiều người kêu là mềm dẻo, nhưng mà thực ra mềm dẻo tốn tiền. Đây là thú nhận mộc mạc của PHP.

PHP does not require (or support) explicit type definition in variable declaration; a variable’s type is determined by the context in which the variable is used. That is to say, if a string value is assigned to variable $var, $var becomes a string. If an integer value is then assigned to $var, it becomes an integer.

An example of PHP’s automatic type conversion is the multiplication operator ‘*’. If either operand is a float, then both operands are evaluated as floats, and the result will be a float. Otherwise, the operands will be interpreted as integers, and the result will also be an integer. Note that this does not change the types of the operands themselves; the only change is in how the operands are evaluated and what the type of the expression itself is.

Tóm váy lại là loại dữ liệu của biến phụ thuộc vào ngữ cảnh.

Ví dụ:

-$a = ‘false’; ==> đây là string.

-$a = false; ==> đây là boolean.

-$a = ‘0123’ => là string.

-$a = 0123; => là integer, kết quả là 83, vì 0123 bắt đầu bằng 0, tức hệ octal (hệ 8).

Nếu string mà hôn phối với integer thì như sau:

$foo = 5 * "10"; //50

echo $foo; // Kết quả vẫn ra 50. Bó tay

$foo = 1 + "11 Small Pigs"; 

echo $foo; // Vẫn ra 12, kèm với thông báo... lỗi Notice: A non well formed numeric value encountered 

ÉP KIỂU (Type casting)

Ép kiểu giúp ta BUỘC giá trị của biến là loại nào đó. Dùng var_dump() để kiểm tra KIỂU DỮ LIỆU của biến và dùng settype() để ép kiểu.

Cách ép kiểu quen thuộc vẫn là dùng keyword ép kiểu. Cụ thể như sau:

Cách ép kiểu (type casting) của PHP tương tự C: dùng tên của loại dữ liệu đặt trong ngoặc đơn trước biến muốn ép kiểu.

<?php
$foo = 10;   // $foo is an integer
$bar = (boolean) $foo;   // $bar is a boolean --> ép kiểu sang boolean.
?>

Các dạng ép kiểu được chấp nhận trong PHP:

  • (int), (integer) – cast to integer
  • (bool), (boolean) – cast to boolean
  • (float), (double), (real) – cast to float
  • (string) – cast to string
  • (array) – cast to array
  • (object) – cast to object
  • (unset) – cast to NULL (sẽ bị loại bỏ sớm)

2. TOÁN TỬ SO SÁNH TRONG PHP

So sánh tương đương == trong PHP

So sánh tương đồng === trong PHP

Có lẽ phải bổ sung CÁC HÀM kiểu tra luận lý BOOLEAN

  • is_bool($var):
  • is_integer($var):
  • is_double($var)
  • is_string($var):
  • is_object($var):
  • is_array($var):
  • is_resource($var): trả về true nếu biến $var là kết nối tới database hay file, …
  • is_null($var): trả về true nếu biến $var chưa được gán giá trị.

Filed Under: Basic, PHP

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2020 · Focus Pro on Genesis Framework · WordPress · Log in