欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PHP中的string類型使用說明

 更新時間:2010年07月27日 19:24:33   作者:  
string就是一串連續(xù)的字符。
注意:PHP沒有對string的長度做限制。唯一限制的就是PHP在計算機中的可用內存(php.ini文件中的memory_limit變量的值)
限定字符串范圍的方法有4中:
1、單引號;
2、雙引號;
3、原型文檔語法;
4、nowdoc syntax(PHP5.3.0開始)

1、如果字符串使用單引號“‘”包裹,字符串中如果出現(xiàn)單引號“,”和反斜杠“\”符號,需要進行轉義。
復制代碼 代碼如下:

// Outputs: Arnold once said: "I'll be back"
echo 'Arnold once said: "I\'ll be back"';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\\*.*?';
// Outputs: You deleted C:\*.*?
echo 'You deleted C:\*.*?';

(有待驗證 單引號包裹的字符串反斜杠是否需要轉義)

2、如果字符串被雙引號包裹 一下字符都會被轉義:
Escaped characters Sequence Meaning
\n linefeed (LF or 0x0A (10) in ASCII)
\r carriage return (CR or 0x0D (13) in ASCII)
\t horizontal tab (HT or 0x09 (9) in ASCII)
\v vertical tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5)
\f form feed (FF or 0x0C (12) in ASCII) (since PHP 5.2.5)
\\ backslash
\$ dollar sign
\" double-quote
\[0-7]{1,3} the sequence of characters matching the regular expression is a character in octal notation
\x[0-9A-Fa-f]{1,2} the sequence of characters matching the regular expression is a character in hexadecimal notation

如果字符串 使用雙引號“"”或者原形文檔語法的形式包裹的話,在字符串中的變量會被解析。
1、簡單語法:
因為解析器會貪婪匹配$后面的字符,所以,為了不出什么以外,應該使用"{"和"}"來表名變量的邊界。
復制代碼 代碼如下:

<?php
$beer = 'Heineken';
echo "$beer's taste is great"; // works; "'" is an invalid character for variable names
echo "He drank some $beers"; // won't work; 's' is a valid character for variable names but the variable is "$beer"
echo "He drank some ${beer}s"; // works
echo "He drank some {$beer}s"; // works
?>

同樣,數(shù)組的下標和對象的屬性也會不解析。
復制代碼 代碼如下:

<?php
// These examples are specific to using arrays inside of strings.
// When outside of a string, always quote array string keys and do not use
// {braces}.
// Show all errors
error_reporting(E_ALL);
$fruits = array('strawberry' => 'red', 'banana' => 'yellow');
// Works, but note that this works differently outside a string
echo "A banana is $fruits[banana].";
// Works
echo "A banana is {$fruits['banana']}.";
// Works, but PHP looks for a constant named banana first, as described below.
echo "A banana is {$fruits[banana]}.";
// Won't work, use braces. This results in a parse error.
echo "A banana is $fruits['banana'].";
// Works
echo "A banana is " . $fruits['banana'] . ".";
// Works
echo "This square is $square->width meters broad.";
// Won't work. For a solution, see the complex syntax.
echo "This square is $square->width00 centimeters broad.";
?>

2、復合語法:
復制代碼 代碼如下:

<?php
// Show all errors
error_reporting(E_ALL);
$great = 'fantastic';
// Won't work, outputs: This is { fantastic}
echo "This is { $great}";
// Works, outputs: This is fantastic
echo "This is {$great}";
echo "This is ${great}";
// Works
echo "This square is {$square->width}00 centimeters broad.";
// Works
echo "This works: {$arr[4][3]}";
// This is wrong for the same reason as $foo[bar] is wrong outside a string.
// In other words, it will still work, but only because PHP first looks for a
// constant named foo; an error of level E_NOTICE (undefined constant) will be
// thrown.
echo "This is wrong: {$arr[foo][3]}";
// Works. When using multi-dimensional arrays, always use braces around arrays
// when inside of strings
echo "This works: {$arr['foo'][3]}";
// Works.
echo "This works: " . $arr['foo'][3];
echo "This works too: {$obj->values[3]->name}";
echo "This is the value of the var named $name: {${$name}}";
echo "This is the value of the var named by the return value of getName(): {${getName()}}";
echo "This is the value of the var named by the return value of \$object->getName(): {${$object->getName()}}";


訪問,修改字符串中的指定字符:
字符串可以使用"[]"和"{}"進行訪問。(注意:php5.3.0以后不建議使用“{}”訪問)
注意:使用其他類型(非integer)類型訪問字符串指定的字符,都會返回NULL
警告:
Writing to an out of range offset pads the string with spaces. Non-integer types are converted to integer. Illegal offset type emits E_NOTICE. Negative offset emits E_NOTICE in write but reads empty string. Only the first character of an assigned string is used. Assigning empty string assigns NUL byte。

相關文章

  • php封裝的數(shù)據(jù)庫函數(shù)與用法示例【參考thinkPHP】

    php封裝的數(shù)據(jù)庫函數(shù)與用法示例【參考thinkPHP】

    這篇文章主要介紹了php封裝的數(shù)據(jù)庫函數(shù)與用法,基于thinkPHP中數(shù)據(jù)庫操作相關代碼整理簡化而來,包括針對數(shù)據(jù)庫的設置、連接、查詢及日志操作等功能,簡單實用,需要的朋友可以參考下
    2016-11-11
  • php 無限分類的樹類代碼

    php 無限分類的樹類代碼

    php tree 無限分類代碼,需要的朋友可以參考下。
    2009-12-12
  • 詳談PHP編碼轉換問題

    詳談PHP編碼轉換問題

    本文給大家分享的是個人對于PHP編碼轉換問題的理解以及處理方法,非常的簡單實用,有需要的小伙伴可以參考下。
    2015-07-07
  • 強烈聲明: 不要使用(include/require)_once

    強烈聲明: 不要使用(include/require)_once

    本篇文章是對不要使用(include/require)_once的原因進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • PHP簡單實現(xiàn)“相關文章推薦”功能的方法

    PHP簡單實現(xiàn)“相關文章推薦”功能的方法

    這篇文章主要介紹了PHP簡單實現(xiàn)“相關文章推薦”功能的方法,方法簡單功能實用,需要的朋友可以參考下
    2014-07-07
  • PHP應用跨時區(qū)功能的實現(xiàn)方法

    PHP應用跨時區(qū)功能的實現(xiàn)方法

    今天小編就為大家分享一篇關于PHP應用跨時區(qū)功能的實現(xiàn)方法,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-03-03
  • PHP 錯誤之引號中使用變量

    PHP 錯誤之引號中使用變量

    當看到錯誤提示 syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING
    2009-05-05
  • php開發(fā)環(huán)境配置記錄

    php開發(fā)環(huán)境配置記錄

    我一般都是自己配置環(huán)境,為了方便在異地快速配置,總結一下,備忘.如果大家感覺麻煩可以一些php運行環(huán)境軟件。例如phpnow等。
    2011-01-01
  • 關于PHP語言構造器介紹

    關于PHP語言構造器介紹

    你提到的“語言構造器”,英文是”language construct”,是語言構成的意思,翻譯成語言構造器難免有點令人困惑
    2013-07-07
  • header與緩沖區(qū)之間的深層次分析

    header與緩沖區(qū)之間的深層次分析

    實際的開發(fā)中,大家是否聽說過在header之前不能有任何的實際輸出。甚至有的認為header函數(shù)必須寫在代碼的最前面??墒悄闶欠裨囼炦^header函數(shù)之前輸出東西?下來讓我們更深層次的了解一下
    2016-07-07

最新評論