<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                ## 前言 這次,我們將演示如何在PHP擴展中如何對類型進行一些操作。如,判斷變量類型。要實現的PHP代碼如下: ``` <?php function get_size ($value) { if (is_string($value)) { return "string size is ". strlen($value); } else if (is_array($value)) { return "array size is ". sizeof($value); } else { return "can not support"; } } var_dump(get_size("abc")); var_dump(get_size(array(1,2))); ?> ``` 分別獲取`string` 和 `array`的長度。 ## 代碼 ### 基礎代碼 這個擴展,我們將在`say`擴展上增加 `get_size` 方法。`say`擴展相關代碼大家請看[這篇博文](/lifei6671/php-extention-dev/672583)。PHP7擴展開發之hello word 文中已經詳細介紹了如何創建一個擴展和提供了源碼下載。 ### 實現get_size方法 get_size方法的PHP擴展源碼: ```c PHP_FUNCTION(get_size) { zval *val; size_t size; zend_string *result; HashTable *myht; if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &val) == FAILURE) { return; } if (Z_TYPE_P(val) == IS_STRING) { result = strpprintf(0, "string size is %d", Z_STRLEN_P(val)); } else if (Z_TYPE_P(val) == IS_ARRAY) { myht = Z_ARRVAL_P(val); result = strpprintf(0, "array size is %d", zend_array_count(myht)); } else { result = strpprintf(0, "can not support"); } RETURN_STR(result); } ``` ## 代碼說明 zval變量相關的宏方法大部分定義在 [Zend/zend_types.h](https://github.com/php/php-src/blob/master/Zend/zend_types.h) 文件中。 ### 類型相關宏方法 `Z_TYPE_P(zval *) `獲取zval變量的類型。常見的類型都有: ```c #define IS_UNDEF 0 #define IS_NULL 1 #define IS_FALSE 2 #define IS_TRUE 3 #define IS_LONG 4 #define IS_DOUBLE 5 #define IS_STRING 6 #define IS_ARRAY 7 #define IS_OBJECT 8 #define IS_RESOURCE 9 #define IS_REFERENCE 10 ``` `Z_STRLEN_P(zval *)` 獲取字符串的長度。 ### 數組 在 Zend/zend_hash.c文件中包含一些array處理的方法。 zend_array_count(HashTable *) 獲取數組的元素個數。 zend_array 和 HashTable其實是相同的數據結構。在 [Zend/zend_types.h](https://github.com/php/php-src/blob/master/Zend/zend_types.h)文件中有定義。 ```c typedef struct _zend_array HashTable; ``` ### 字符串拼接 `strpprintf`是PHP為我們提供的字符串拼接的方法。第一個參數是最大字符數。 ## PHP7變量相關資料 在PHP7中對于zval變量的結構有了不小的改動。大家可以查看下面三篇文章。介紹的比較詳細。 https://github.com/laruence/php7-internal/blob/master/zval.md http://0x1.im/blog/php/Internal-value-representation-in-PHP-7-part-1.html http://0x1.im/blog/php/Internal-value-representation-in-PHP-7-part-2.html
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看