第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

具有多個(gè)/超過 3 個(gè)條件的 php if 和 elseif 語句

具有多個(gè)/超過 3 個(gè)條件的 php if 和 elseif 語句

PHP
嗶嗶one 2022-01-02 19:49:02
我想在用戶在網(wǎng)站上注冊(cè)時(shí)為其分配隨機(jī)的個(gè)人資料圖片。我的代碼有問題。如果我將“if 和 elseif”語句限制為 2 個(gè)條件,則效果很好。但是當(dāng)我超過 2(在這種情況下我有 10 個(gè)條件)時(shí),代碼不起作用。如果應(yīng)該使用“switch”語句來代替,您將如何編寫代碼?這是我的代碼$rand = rand(1,10); //random number between 1 and 10if($rand == 1)$profile_pic = "/defaults/profile_pic1.png";else if($rand == 2)$profile_pic = "/defaults/profile_pic2.png";...else if($rand == 9)$profile_pic = "/defaults/profile_pic9.png";else$profile_pic = "/defaults/profile_pic10.png";
查看完整描述

3 回答

?
精慕HU

TA貢獻(xiàn)1845條經(jīng)驗(yàn) 獲得超8個(gè)贊

我希望這就是你想要的


<?php

$rand = rand(1,10);

switch ($rand) {

    case "1":

        $profile_pic = "/defaults/profile_pic1.png";

        echo "1";

        break;

    case "2":

        $profile_pic = "/defaults/profile_pic2.png";

        echo "2";

        break;

    case "3":

        $profile_pic = "/defaults/profile_pic3.png";

        echo "3";

        break;

    case "4":

        $profile_pic = "/defaults/profile_pic4.png";

        echo "4";

        break;

    case "5":

        $profile_pic = "/defaults/profile_pic5.png";

        echo "5";

        break;

    case "6":

        $profile_pic = "/defaults/profile_pic6.png";

        echo "6";

        break;

    case "7":

        $profile_pic = "/defaults/profile_pic7.png";

        echo "7";

        break;

    case "8":

        $profile_pic = "/defaults/profile_pic8.png";

        echo "8";

        break;

    case "9":

        $profile_pic = "/defaults/profile_pic9.png";

        echo "9";

        break;

    case "10":

        $profile_pic = "/defaults/profile_pic10.png";

        echo "10";

        break;

    default:

        $profile_pic = "/defaults/profile_picDEFAULT.png";

        echo "default PHOTO";

}

?>


查看完整回答
反對(duì) 回復(fù) 2022-01-02
?
收到一只叮咚

TA貢獻(xiàn)1821條經(jīng)驗(yàn) 獲得超5個(gè)贊

我真的不明白你為什么要在switch這里使用聲明。在改進(jìn)代碼方面 - 您只能使用 2 行。隨著if和switch它的每個(gè)條件2-3行,它是最壞的編程習(xí)慣你可能會(huì)拿出。只要給我一個(gè)很好的理由,為什么。


如果您仔細(xì)閱讀 PHP 文檔,則可以使用字符串運(yùn)算符頁面,其中解釋了如何解決您的問題:


連接運(yùn)算符 ( .),它返回其左右參數(shù)的連接。


$a = "Hello ";

$b = $a . "World!"; // now $b contains "Hello World!"

所以你可以簡(jiǎn)單地用


$rand = rand(1,10);

$profile_pic = "/defaults/profile_pic" . $rand . ".png";

或者你可能會(huì)偶然發(fā)現(xiàn)Variable parsing,它指出:


當(dāng)一個(gè)字符串用雙引號(hào)或 heredoc 指定時(shí),變量在其中解析。


$juice = "apple";

echo "He drank some $juice juice.".PHP_EOL;

// Invalid. "s" is a valid character for a variable name, but the variable is $juice.

echo "He drank some juice made of $juices.";

// Valid. Explicitly specify the end of the variable name by enclosing it in braces:

echo "He drank some juice made of ${juice}s.";

所以你可以做的是:


$rand = rand(1,10);

$profile_pic = "/defaults/profile_pic${rand}.png";


查看完整回答
反對(duì) 回復(fù) 2022-01-02
?
慕的地8271018

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超4個(gè)贊

如果您在圖像路徑中使用相同的隨機(jī)數(shù),則不需要 if 或 switch 循環(huán)。您可以使用以下代碼。$rand = rand(1,10) ;

$profile_pic = "/defaults/profile_pic$rand.png"; 如果您有隨機(jī)數(shù)和圖像的映射,則將其存儲(chǔ)在數(shù)組中并訪問它。$rand_image = array(1=>"firstimg.png", 2=>"2.png") ;

$profile_pic = "/defaults/profile_pic".$rand_image[$rand];


查看完整回答
反對(duì) 回復(fù) 2022-01-02
  • 3 回答
  • 0 關(guān)注
  • 322 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)