PHP基本语法读书笔记--字符串与常量

strlen() 函数

1,strlen() 函数返回字符串的长度,以字符计。

strlen() 常用于循环和其他函数,在确定字符串何时结束很重要时。(例如,在循环中,我们也许需要在字符串的最后一个字符之后停止循环)。

2,str_word_count() 函数对字符串中的单词进行计数:

3,strrev() 函数反转字符串

4,strpos() 函数用于检索字符串内指定的字符或文本。

如果找到匹配,则会返回首个匹配的字符位置。如果未找到匹配,则将返回 FALSE。

5,str_replace() 函数用一些字符串替换字符串中的另一些字符.

<!DOCTYPE html>
<html>
<body>

<?php
echo '<br>';
echo strlen('life can be wonderful');
echo '<br>';
echo str_word_count('life can be wonderful');
echo '<br>';
echo strrev('life can be wonderful');
echo '<br>';
echo strpos("It is more than a word that can be a true love of symbol!","true");
echo '<br>';
echo str_replace("wonderful","happiness",'life can be wonderful');
echo '<br>';
?>

</body>
</html>

常量

常量是单个值的标识符(名称)。在脚本中无法改变该值。

有效的常量名以字符或下划线开头(常量名称前面没有 $ 符号)。

注释:与变量不同,常量贯穿整个脚本是自动全局的。

如需设置常量,请使用 define() 函数 - 它使用三个参数:

  1. 首个参数定义常量的名称
  2. 第二个参数定义常量的值
  3. 可选的第三个参数规定常量名是否对大小写不敏感。默认是 false。
<?php
define("GREETING", "Welcome to ideashare.club!");
echo GREETING;
?>

 常量是自动全局的,而且可以贯穿整个脚本使用。

发布了192 篇原创文章 · 获赞 29 · 访问量 45万+

猜你喜欢

转载自blog.csdn.net/julius_lee/article/details/88419684