美文网首页
数据类型转换,循环体

数据类型转换,循环体

作者: overisover | 来源:发表于2017-02-15 09:06 被阅读0次

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 />';
?>

相关文章

  • 数据类型转换,循环体

    PHP其余数据类型转换为逻辑型 强制转换:settype(变量名,类型);隐式转换,根据语句转换*数字——bool...

  • JAVA基础第四天

    JAVA数据类型---布尔类型; 数据类型转换自动数据类型转换 强制数据类型转换

  • JavaScript基础03- 数据类型转换

    数据类型转换 所谓的数据类型转换,就是将一种数据类型转换为另一种数据类型,所以,数据类型转换包括,转换为字符串类型...

  • JS里的数据类型转换

    在js中,数据类型转换分为显式数据类型转换和隐式数据类型转换。 1, 显式数据类型转换 a:转数字: 1)Numb...

  • javascript-强制类型转换和运算符

    强制类型转换 将其它数据类型转换为String 将其它数据类型转换为Number 将其它数据类型转换为Boolea...

  • Python自学笔记——3.基础数据类型转换

    基础数据类型转换 数据类型转换的形式自动类型转换强制类型转换 可变数据类型: 列表, 字典, 集合不可变数...

  • 3-数据类型转换

    数据类型转换 将数据有当前类型变化为其他类型的操作就是数据类型转换。 数据类型转换分类: 数据类型转换一共分为2类...

  • Java基础语法_数据类型转换

    数据类型转换 当数据类型不一样时,将会发生数据类型转换。 数据类型转换——自动转换(隐式) 特点:代码不需要进行特...

  • 其他类型转换成字符串类型

    数据类型强制转换 字符串相关数据类型转换

  • JavaScript字符串转换为其他类型(五)

    目录: 为什么要数据类型转换? 数据类型怎么转换? 一、为什么要数据类型转换? 上节课咱们讲到了数据类型好比人的职...

网友评论

      本文标题:数据类型转换,循环体

      本文链接:https://www.haomeiwen.com/subject/rohxwttx.html