数据类型转换,循环体

PHP其余数据类型转换为逻辑型

强制转换:settype(变量名,类型);隐式转换,根据语句转换*
数字——boolean: 0,0.0为false ,其余为true //与js一致
字符串——boolean 值为0和空 结果为false ;其余为true
数组——boolean 数组为空 结果为false ;其余为true
对象——boolean 都为true ;
null——boolean 结果为false;
resource——boolean 都为true ;
//js中复合数据转换为boolean 均为true;null ,undefined 为false;
创建一个对象

$student = new stdClass();  //创建一个对象
//在php中访问对象的属性或者方法用 ->
//$student->sex = '男';
//$student->name = '雷雷';
//var_dump($student);
settype($student,'boolean');
var_dump($student);//true;

empty(变量);判断变量是否为空;//除了对象为空是false,其余空值或0或false为true

isset(变量);判断变量是否已经声明;//变量为null时,或者变量未申明,结果为false;
unset(变量);销毁变量

<?php
//强制转换:settype(变量名,类型)
$total='';//值为空
settype($total,'boolean');
var_dump($total);//false;
unset($total);//销毁变量
var_dump(isset($total));//false;
var_dump(empty($total));//true;
?>

查找字符串是否包含某字符串

区分大小写:strpos($str,'b');查找$str中是否包含b;找到返回索引,未找到返回false ;
不区分大小写:stripos($str,'b');

<?php
$str = 'HO,TMD,today is bad!';
$str = 'TMD,today is bad!';
echo strpos($str,'b').'<br />';//位置13;
var_dump(strpos($str,'B'));false
var_dump(stripos($str,'B'));  //不区分大小写,int(13) 
echo '<br />';
?>

替换字符串中指定的子字符串为新的字符串;

str_replace();返回新的字符串;不影响原字符串;
$new_str=str_replace(查找的子字符串,替换的新字符串,原字符串);
$str='asc';
$new_str=str_replace('a','*',$str);
比较运算符:!=;:不等于;!==:不全等 值等,类型不等为真

if(is_string($str) && strpos($str,$find)!==false){
    //有bug,当查找值为0时
    $str = str_replace($find,'*',$str);
}
echo $str.'<hr />';

三目表达式

当if,else 语句块中只有一条时最好转换为三目表达式
$result=条件?A:B;

使用变量,先准备变量,拼接PHP;

<?php
if(!empty($hasBgColor)){//当满足条件是执行下面代码,不满足是不执行
?>
<script>
    document.body.style.background="<?=$color?>";
</script>
<?
}
?>

根据图片后缀设置不同格式

$filePath='image/logo.jpg'; $result=pathinfo($filePath,PATHINFO_EXTENSION);有第二个参数,可以直接获取字符串; $result=pathinfo($filePath);//返回路径文件夹名:dirname,文件名+后缀名:basename,后缀名:extension,文件名:filename; //var_dump($result);//数组用var_dump打印 $ext=$result['extension'];//jpg 获取后缀名;
strtolower(字符串)——大写字母转小写字母;
strtoupper(字符串)——小写字母转大写字母;
ucfirst();将整句话的首字母大写
ucwords();将单词的首字母大写
echo ucwords('hello world avd');

<?php
$imgPath='image/logo.jpg';
$imgPath='image/logo.png';
$imgPath='image/logo.gif';
if(strpos($imgPath,'.jpg')>0){
    $width=200;
    $height=300;
    $border='1px solid #c2c';
}else if(strpos($imgPath,'.png')>0){
    $width=100;
    $height=100;
    $border='2px solid #c0c';
}else if(strpos($imgPath,'.gif')>0){
    $width=50;
    $height=60;
    $border='2px solid #c0c';
}

?>
<img src="<?=$imgPath?>" alt="" width="<?=$width?>" height="<?=$height?>" style="border:<?=$border?>">

分支语句格式按后缀名不同设置不同图片格式;

有明确的值,用switch;取值范围用if,else if

<?php
$imgPath='image/logo.jpg';
//$imgPath='image/logo.png';
//$imgPath='image/logo.gif';
$result=pathinfo($imgPath);//result是数组;
$ext=$result['extension'];
switch ($ext){//有明确的值,用switch;取值范围用if,else if
    case 'png':
        $width=200;
        $height=300;
        $border='1px solid #c2c';
        break;
    case  'mp4':
    case  'jpg'://或者mp4
        $width=100;
        $height=100;
        $border='2px solid #c0c';
        break;
    case  'gif':
        $width=50;
        $height=60;
        $border='2px solid #c0c';
        break;
    default :
        $width=50;
        $height=60;
        $border='2px solid #c0c';
};
?>
<img src="<?=$imgPath?>" alt="" width="<?=$width?>" height="<?=$height?>" style="border:<?=$border?>">

for循环,输出星星;

<?php
$max=5;
for($r=1;$r<=$max;$r++){
    for($s=0;$s<=$r-1;$s++){
        echo '  ';
    }
    for($st=$r-1;$st<=2*$max-1-$r;$st++){
        echo '*';
    }
    echo '<br/>';
}
?>

<h2>找出两个数之间的所有质数,并将这些数放入li标签中</h2>

<ul style="list-style: none;">
    <?php
        for($num=2;$num<100;$num++){
            $is=0;//代表质数;
            for($i=2;$i<$num;$i++){
                if($num%$i==0){
                    $is=1;//代表非质数;
                    break;
                }
            }
            if( $is===0){//判断是否是质数,输出值;
                ?>
                <li><?=$num?></li>
                <?
            }
        }
    ?>  
</ul>

while ,do{}while();

continue ,break;

continue:循环中,作用,结束当次循环,继续下一次循环;
break:循环中,作用 ,结束所在循环;

<?php
//continue用于循环中,作用是结束当次循环继续下一次循环开始处
//将1~20之间的所有数相乘,其中是3的倍数的数跳过  2*3*6
$result = 1;
for($number=1;$number<10;$number++){
    if($number%3==0) continue;
    echo $number.'<br />';
    $result*=$number;
}
echo $result.'<hr />';

//break用于循环中,作用是结束整个循环
//将1~20之间的所有数相乘,如果乘积大于了10000便结束相乘


$result = 1;
for($number=1;$number<20;$number++){
    if($result>10000) break;
    echo $number.'<br />';
    $result*=$number;
}
echo $result.'<hr />';
?>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容