• 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

É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.

[Read more…]

Filed Under: Basic, PHP

echo string lồng multidimensional array

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

Tưởng echo mà ngon à, thử gắn với array biết nhau ngay.

Ví dụ có array lồng sau:

// array lồng
$array = array(
array('id' => 1, 'name' => 'cấp 1' , 'parent' => 'cha mẹ index 0'),
array('id' => 2, 'name' => 'cấp 2' , 'parent' => 'cha mẹ 1'),

);

Phân tích thì đây là một array dạng lồng multidimensional.

Thường để lấy hết giá trị array ta dùng hàm foreach, hoặc for. Ví dụ:

foreach ($array as $array) {
echo $array['parent'];
}

Cách đơn giản sau hỗ trợ xuất giá trị nếu xác định được key=> value của array, kể cả array lồng. Ví dụ:

echo $array[0]['name'];
// chú ý index dạng số của array bắt đầu bằng 0 :-)

Câu hỏi là làm sao để echo giá trị của array lồng trong một string.

Câu trả lời là Complex (curly) syntax.

Định nghĩa thần thánh như sau:

This isn’t called complex because the syntax is complex, but because it allows for the use of complex expressions.

Any scalar variable, array element or object property with a string representation can be included via this syntax. Simply write the expression the same way as it would appear outside the string, and then wrap it in { and }. Since { can not be escaped, this syntax will only be recognised when the $ immediately follows the {. Use {\$ to get a literal {$.

Ta có ví dụ sau:

echo "ví dụ {$array[0]['name']} {$array[0]['parent']}";
// Kết quả: 'ví dụ cấp 1 cha mẹ index 0'

Như vậy dùng cặp ngoặc chéo {} để “bo” biến trong string, biến lúc này là một value lấy từ array lồng.

Noted: Có thể thấy {} bo được biến, element của array và  cả thuộc tính của đối tượng (object property).

Filed Under: Basic, PHP

Các magic methods của PHP khi lập trình OOP, trường hợp __toString() và __invoke()

Posted on April 9, 2018 Written by admin 1 Comment

Đây là các phương thức huyền diệu, aka magic methods dùng với OOP khi mần PHP:

__construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone() and __debugInfo()

Hay dùng nhất có lẽ là __construct(), hàm này cho phép tự động khởi tạo và gán giá trị mỗi lần một object mới được tạo từ class đã dựng sẵn. Hàm __construct ()truyền tham số (arguments) từa lưa nên rất tiện.

[Read more…]

Filed Under: OOP

Hằng trong PHP

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

Hằng là một kiểu biến, chứa dữ liệu trong PHP. Giá trị này không thể thay đổi và không thể xóa (unset). Hằng thường được viết hoa. Hằng có giá trị toàn cục (global scope).

Để gọi danh sách hằng dùng hàm get_defined_constants(), gía trị trả về là array cho biết tất cả các hằng hiện có.

Syntax của hằng

Có hai cách thiết lập hằng:

Cách 1: define(‘HẰNG’, ‘giá trị của hằng’);

Cả ‘HẰNG’ và ‘Giá trị của hằng’ nếu là string phải đặt trong ngoặc đơn hoặc kép, nếu là integer thì không cần.

[Read more…]

Filed Under: Basic, PHP

  • « Previous Page
  • 1
  • 2
  • 3
  • 4
  • Next Page »

Recent Posts

  • How to pass a variable to the callback function provided by Laravel Collection
  • Cài Sodium trên PHP 7.2 Centos
  • Dependency Injection và Container
  • Hàm array_map() và array_walk()
  • Dependency Injection

Recent Comments

  • admin on Dependency Injection
  • Vượng on Dependency Injection
  • vượng on Dependency Injection
  • vượng on Dependency Injection
  • vượng on Dependency Injection

Archives

  • July 2018
  • June 2018
  • May 2018
  • April 2018

Categories

  • Bài tập PHP
  • Basic
  • Laravel
  • OOP
  • OOP
  • PHP
  • String
  • Uncategorized
  • WordPress

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

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