Làm cách nào để làm mới một biến trong PHP?

Sau khi thực hiện các câu lệnh trên, biến $txt sẽ giữ giá trị

    $session = $this->getRequest[]->getSession[];
    if [$page == 'nc' && Configure::read['NC.installed']] { 
        $session->write['module', 'nc'];
        $modname = Configure::read['headertitle'] . ": " . Configure::read['NC.name'] . " Module";
        Configure::write['headertitle', $modname]; 
    } else if [$page == 'pm' && Configure::read['PM.installed']] {
        $session->write['module', 'pm'];
        $modname = Configure::read['headertitle'] . ": " . Configure::read['PM.name'] . " Module";
        Configure::write['headertitle', $modname];
0, biến
    $session = $this->getRequest[]->getSession[];
    if [$page == 'nc' && Configure::read['NC.installed']] { 
        $session->write['module', 'nc'];
        $modname = Configure::read['headertitle'] . ": " . Configure::read['NC.name'] . " Module";
        Configure::write['headertitle', $modname]; 
    } else if [$page == 'pm' && Configure::read['PM.installed']] {
        $session->write['module', 'pm'];
        $modname = Configure::read['headertitle'] . ": " . Configure::read['PM.name'] . " Module";
        Configure::write['headertitle', $modname];
1 sẽ giữ giá trị
    $session = $this->getRequest[]->getSession[];
    if [$page == 'nc' && Configure::read['NC.installed']] { 
        $session->write['module', 'nc'];
        $modname = Configure::read['headertitle'] . ": " . Configure::read['NC.name'] . " Module";
        Configure::write['headertitle', $modname]; 
    } else if [$page == 'pm' && Configure::read['PM.installed']] {
        $session->write['module', 'pm'];
        $modname = Configure::read['headertitle'] . ": " . Configure::read['PM.name'] . " Module";
        Configure::write['headertitle', $modname];
2 và biến
    $session = $this->getRequest[]->getSession[];
    if [$page == 'nc' && Configure::read['NC.installed']] { 
        $session->write['module', 'nc'];
        $modname = Configure::read['headertitle'] . ": " . Configure::read['NC.name'] . " Module";
        Configure::write['headertitle', $modname]; 
    } else if [$page == 'pm' && Configure::read['PM.installed']] {
        $session->write['module', 'pm'];
        $modname = Configure::read['headertitle'] . ": " . Configure::read['PM.name'] . " Module";
        Configure::write['headertitle', $modname];
3 sẽ giữ giá trị
    $session = $this->getRequest[]->getSession[];
    if [$page == 'nc' && Configure::read['NC.installed']] { 
        $session->write['module', 'nc'];
        $modname = Configure::read['headertitle'] . ": " . Configure::read['NC.name'] . " Module";
        Configure::write['headertitle', $modname]; 
    } else if [$page == 'pm' && Configure::read['PM.installed']] {
        $session->write['module', 'pm'];
        $modname = Configure::read['headertitle'] . ": " . Configure::read['PM.name'] . " Module";
        Configure::write['headertitle', $modname];
4

Ghi chú. Khi bạn gán một giá trị văn bản cho một biến, hãy đặt dấu ngoặc kép xung quanh giá trị

Ghi chú. Khác với các ngôn ngữ lập trình khác, PHP không có lệnh khai báo biến. Nó được tạo ngay khi bạn gán giá trị cho nó lần đầu tiên

Hãy nghĩ về các biến như các thùng chứa để lưu trữ dữ liệu

Một biến có thể có tên ngắn [như x và y] hoặc tên mô tả hơn [tuổi, carname, total_volume]

Quy tắc cho các biến PHP

  • Một biến bắt đầu bằng dấu
        $session = $this->getRequest[]->getSession[];
        if [$page == 'nc' && Configure::read['NC.installed']] { 
            $session->write['module', 'nc'];
            $modname = Configure::read['headertitle'] . ": " . Configure::read['NC.name'] . " Module";
            Configure::write['headertitle', $modname]; 
        } else if [$page == 'pm' && Configure::read['PM.installed']] {
            $session->write['module', 'pm'];
            $modname = Configure::read['headertitle'] . ": " . Configure::read['PM.name'] . " Module";
            Configure::write['headertitle', $modname];
    
    5, theo sau là tên của biến
  • Tên biến phải bắt đầu bằng một chữ cái hoặc ký tự gạch dưới
  • Tên biến không được bắt đầu bằng số
  • Tên biến chỉ có thể chứa các ký tự chữ và số và dấu gạch dưới [A-z, 0-9 và _ ]
  • Tên biến phân biệt chữ hoa chữ thường [_______16 và
        $session = $this->getRequest[]->getSession[];
        if [$page == 'nc' && Configure::read['NC.installed']] { 
            $session->write['module', 'nc'];
            $modname = Configure::read['headertitle'] . ": " . Configure::read['NC.name'] . " Module";
            Configure::write['headertitle', $modname]; 
        } else if [$page == 'pm' && Configure::read['PM.installed']] {
            $session->write['module', 'pm'];
            $modname = Configure::read['headertitle'] . ": " . Configure::read['PM.name'] . " Module";
            Configure::write['headertitle', $modname];
    
    7 là hai biến khác nhau]

Hãy nhớ rằng tên biến PHP phân biệt chữ hoa chữ thường

Biến đầu ra

Câu lệnh PHP $txt0 thường dùng để xuất dữ liệu ra màn hình

Ví dụ sau sẽ cho thấy cách xuất văn bản và một biến

Ví dụ sau sẽ tạo ra kết quả tương tự như ví dụ trên

Ví dụ sau sẽ xuất ra tổng của hai biến

Ghi chú. Bạn sẽ tìm hiểu thêm về câu lệnh $txt0 và cách xuất dữ liệu ra màn hình trong chương tiếp theo

PHP là một ngôn ngữ được gõ lỏng lẻo

Trong ví dụ trên, hãy lưu ý rằng chúng ta không cần phải cho PHP biết biến đó thuộc kiểu dữ liệu nào.

PHP tự động liên kết kiểu dữ liệu với biến, tùy thuộc vào giá trị của nó. Vì các kiểu dữ liệu không được đặt theo nghĩa chặt chẽ, nên bạn có thể thực hiện những việc như thêm chuỗi vào số nguyên mà không gây ra lỗi

Trong PHP 7, khai báo kiểu đã được thêm vào. Điều này cung cấp một tùy chọn để chỉ định loại dữ liệu dự kiến ​​khi khai báo một hàm và bằng cách bật yêu cầu nghiêm ngặt, nó sẽ đưa ra "Lỗi nghiêm trọng" đối với loại không khớp

Bạn sẽ tìm hiểu thêm về các yêu cầu của $txt2 và $txt3 cũng như các khai báo kiểu dữ liệu trong chương Hàm PHP

                                    function data[] {
        var ret = [];
        ret.push[{
          y: 'Today',
          a: 
        }];
      return ret;
    }

var graph = Morris.Bar[{
        element: 'graph',
        data: data[],
        xkey: 'y',
        ykeys: ['a'],
        labels: ['random label']
    }];

function update[] {
      graph.setData[data[]];
    }

setInterval[update, 60000];

Trong

// Set in PagesController via $session->write[] 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write['module', 'ml'];  

// setMenu[] called in beforeFilter of AppController
$this->setMenu[];   

// 'module' session variable read in AppController::setMenu[];
$module = $session->read['module'];

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
2 của tôi, tôi viết một vài biến phiên và cấu hình, tùy thuộc vào trang mà người dùng đang truy cập. Các giá trị cấu hình đến từ tệp
// Set in PagesController via $session->write[] 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write['module', 'ml'];  

// setMenu[] called in beforeFilter of AppController
$this->setMenu[];   

// 'module' session variable read in AppController::setMenu[];
$module = $session->read['module'];

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
3 của tôi. Biến phiên
// Set in PagesController via $session->write[] 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write['module', 'ml'];  

// setMenu[] called in beforeFilter of AppController
$this->setMenu[];   

// 'module' session variable read in AppController::setMenu[];
$module = $session->read['module'];

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
4 kiểm soát một số mục menu và một vài cài đặt nền khác. Đây là một ví dụ

    $session = $this->getRequest[]->getSession[];
    if [$page == 'nc' && Configure::read['NC.installed']] { 
        $session->write['module', 'nc'];
        $modname = Configure::read['headertitle'] . ": " . Configure::read['NC.name'] . " Module";
        Configure::write['headertitle', $modname]; 
    } else if [$page == 'pm' && Configure::read['PM.installed']] {
        $session->write['module', 'pm'];
        $modname = Configure::read['headertitle'] . ": " . Configure::read['PM.name'] . " Module";
        Configure::write['headertitle', $modname];

… và cứ thế cho mỗi mô-đun được cài đặt trong dự án của tôi. Hệ thống menu được tải từ

// Set in PagesController via $session->write[] 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write['module', 'ml'];  

// setMenu[] called in beforeFilter of AppController
$this->setMenu[];   

// 'module' session variable read in AppController::setMenu[];
$module = $session->read['module'];

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
5 của tôi và dựa vào giá trị hiện tại của
// Set in PagesController via $session->write[] 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write['module', 'ml'];  

// setMenu[] called in beforeFilter of AppController
$this->setMenu[];   

// 'module' session variable read in AppController::setMenu[];
$module = $session->read['module'];

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
4 để tải chính xác các mục menu

Vấn đề. Khi người dùng di chuyển giữa các trang trong dự án của tôi, biến

// Set in PagesController via $session->write[] 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write['module', 'ml'];  

// setMenu[] called in beforeFilter of AppController
$this->setMenu[];   

// 'module' session variable read in AppController::setMenu[];
$module = $session->read['module'];

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
4 chỉ làm mới nếu trang được làm mới thủ công. Ví dụ. Nếu giá trị hiện tại của
// Set in PagesController via $session->write[] 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write['module', 'ml'];  

// setMenu[] called in beforeFilter of AppController
$this->setMenu[];   

// 'module' session variable read in AppController::setMenu[];
$module = $session->read['module'];

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
4 là
// Set in PagesController via $session->write[] 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write['module', 'ml'];  

// setMenu[] called in beforeFilter of AppController
$this->setMenu[];   

// 'module' session variable read in AppController::setMenu[];
$module = $session->read['module'];

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
0 và tôi điều hướng đến
// Set in PagesController via $session->write[] 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write['module', 'ml'];  

// setMenu[] called in beforeFilter of AppController
$this->setMenu[];   

// 'module' session variable read in AppController::setMenu[];
$module = $session->read['module'];

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
1, tôi muốn giá trị của
// Set in PagesController via $session->write[] 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write['module', 'ml'];  

// setMenu[] called in beforeFilter of AppController
$this->setMenu[];   

// 'module' session variable read in AppController::setMenu[];
$module = $session->read['module'];

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
4 cập nhật từ
// Set in PagesController via $session->write[] 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write['module', 'ml'];  

// setMenu[] called in beforeFilter of AppController
$this->setMenu[];   

// 'module' session variable read in AppController::setMenu[];
$module = $session->read['module'];

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
0 thành
// Set in PagesController via $session->write[] 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write['module', 'ml'];  

// setMenu[] called in beforeFilter of AppController
$this->setMenu[];   

// 'module' session variable read in AppController::setMenu[];
$module = $session->read['module'];

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
1 khi tải trang, nhưng giá trị này vẫn ở mức
// Set in PagesController via $session->write[] 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write['module', 'ml'];  

// setMenu[] called in beforeFilter of AppController
$this->setMenu[];   

// 'module' session variable read in AppController::setMenu[];
$module = $session->read['module'];

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
0 cho đến khi tôi làm mới thủ công. Sau đó, menu của tôi hiển thị đúng. Tôi có thể buộc làm mới biến phiên ________ 24 khi tải trang không?

Có thể điều này sẽ giúp. Tôi đã sửa lỗi thêm và hóa ra biến

// Set in PagesController via $session->write[] 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write['module', 'ml'];  

// setMenu[] called in beforeFilter of AppController
$this->setMenu[];   

// 'module' session variable read in AppController::setMenu[];
$module = $session->read['module'];

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
4 đang cập nhật trong PagesController của tôi, nhưng bản cập nhật không được phản ánh trong AppController của tôi

Khi tôi gỡ lỗi

// Set in PagesController via $session->write[] 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write['module', 'ml'];  

// setMenu[] called in beforeFilter of AppController
$this->setMenu[];   

// 'module' session variable read in AppController::setMenu[];
$module = $session->read['module'];

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
4 trong cả hai bộ điều khiển, tôi nhận được hai giá trị khác nhau, trừ khi tôi làm mới trang. Đây là quy trình làm việc

// Set in PagesController via $session->write[] 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write['module', 'ml'];  

// setMenu[] called in beforeFilter of AppController
$this->setMenu[];   

// 'module' session variable read in AppController::setMenu[];
$module = $session->read['module'];

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    

Kết quả gỡ lỗi 'mô-đun' trong cả hai bộ điều khiển, sau khi tôi rời khỏi trang 'doc' và tải trang 'ml'

________số 8

Giá trị của

// Set in PagesController via $session->write[] 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write['module', 'ml'];  

// setMenu[] called in beforeFilter of AppController
$this->setMenu[];   

// 'module' session variable read in AppController::setMenu[];
$module = $session->read['module'];

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
4 trong AppController là mô-đun tôi đã tham gia trước khi nhấp vào 'doc' trong menu của mình. Khi tôi hiển thị trang 'doc', tôi phải làm mới trang để giá trị của
// Set in PagesController via $session->write[] 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write['module', 'ml'];  

// setMenu[] called in beforeFilter of AppController
$this->setMenu[];   

// 'module' session variable read in AppController::setMenu[];
$module = $session->read['module'];

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
4 cập nhật thành 'doc'. Tôi đã giả định rằng, vì
// Set in PagesController via $session->write[] 
// Value changes whenever I click on a different menu item; 'ml' given as example
$module = $session->write['module', 'ml'];  

// setMenu[] called in beforeFilter of AppController
$this->setMenu[];   

// 'module' session variable read in AppController::setMenu[];
$module = $session->read['module'];

// value of 'module' used in $conditions to make sure proper menu options load
$conditions = ['OR' => [
            ['group_prefix' => 'all'], 
            ['module' => $module],
        ]];    
4 là một biến phiên nên nó sẽ cập nhật trên ứng dụng của tôi khi nó thay đổi. Vì vậy, những gì tôi cần là để nó cập nhật trong AppController của tôi cùng lúc nó cập nhật trong PagesController của tôi. Nhưng tôi đoán nó cập nhật chính xác ở phần sau vì nó thực sự được viết ở đó?

Làm cách nào để làm mới dữ liệu trong PHP?

Sử dụng hàm header[] để làm mới trang web bằng PHP. Các chức năng HTTP là những chức năng thao tác thông tin được máy chủ Web gửi đến máy khách hoặc trình duyệt trước khi bất kỳ đầu ra nào khác được gửi đi.

Làm cách nào để đặt lại một biến trong PHP?

Hàm unset[] bỏ đặt biến.

Làm cách nào để đặt tự động làm mới trong PHP?

Bạn có thể tự động làm mới PHP bằng cách sử dụng thẻ meta HTML, chức năng tiêu đề PHP, vị trí. href thuộc tính hoặc vị trí. hàm tải lại[] của JavaScript .

Làm cách nào để đặt lại biến toàn cục trong PHP?

Hàm unset[] trong PHP đặt lại bất kỳ biến nào. Nếu unset[] được gọi bên trong một hàm do người dùng định nghĩa, nó sẽ hủy đặt các biến cục bộ. Nếu người dùng muốn hủy đặt biến toàn cục bên trong hàm thì họ phải sử dụng mảng $GLOBALS để làm như vậy. Hàm unset[] không có giá trị trả về.

Chủ Đề