php 基础笔记 - string

本文介绍PHP中字符串的使用方法,包括单引号、双引号字符串的区别,变量解析方式,以及heredoc语法示例。通过具体代码展示不同类型的字符串如何定义与使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/***************************by garcon1986********************************/

<?php // example for strings, single quoted, double quoted echo 'display a string!<br>'; echo ' this displays a splitted string<br>'; echo 'i/'ll be "back"<br>'; echo 'she said:"i/'ll be back!"<br>'; echo 'the path is c:/programmes/sjg/*.*!'.''.'hello world<br>'; echo 'the path is c://prgralles//sjg/*.*<p>'; $h = 'hellos'; $i = "hellos"; echo '$h<br>'; echo "$h"."<br>"; echo '$i<br>'; echo "$i<br>"; //heredoc example $str = <<<EOD Example of string spanning multiple lines using heredoc syntax. EOD; /* More complex example, with variables. */ class foo { var $foo; var $bar; function foo() { $this->foo = 'Foo'; $this->bar = array('Bar1', 'Bar2', 'Bar3'); } } $foo = new foo(); $name = 'MyName'; echo <<<EOT My name is "$name". I am printing some $foo->foo. Now, I am printing some {$foo->bar[1]}. This should print a capital 'A': /x41 EOT; echo '$str<br>'; echo "$str<p>"; // variable parsing $beer = "xuehua"; echo "$beer's taste is good<br>";// works; "'" is an invalid character for variable names echo "he drinks some $beers<br>";// won't work; 's' is a valid character for variable names but the variable is "$beer echo "he drinks some ${beer}s<br>";//works echo "he drinks some {$beer}s<br>";//works error_reporting(E_ALL); $fruits = array('strawberry' => 'red', 'banana' => 'yellow'); echo "A banana is $fruits[banana].<br>"; echo "A banana is {$fruits['banana']}.<br>"; echo "A banana is {$fruits[banana]}.<br>"; // Works but PHP looks for a constant named banana first as described below. //echo "A banana is . $fruits['banana'].<br>"; echo "A banana is " . $fruits['banana'] . ".<br>"; echo "This square is $square->width meters broad.<br>"; echo "This square is $square->width00 centimeters broad.<br>"; $foo = array('a'=> 11, 3 => true); // more complex syntax $great = 'fantastic'; echo "This is { $great}<br>"; // Won't work, outputs: This is { fantastic} echo "This is {$great}<br>";// Works, outputs: This is fantastic echo "This is ${great}<br>";// Works, outputs: This is fantastic echo "This square is {$square->width}00 centimeters broad.<br>"; echo "This works: {arr[foo][3]}<br>"; echo "This works: {$arr['foo'][3]}<br>"; echo "This is wrong: {$arr['foo'][3]}<br>"; ?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值