2016-05-13 97 views
0

實際上做我見過很多PHP函數&許多PHP腳本,我總是覺得什麼是真正的錯誤在PHP

function check() { 
    if(isset($_POST['example'])){ 
     return true; 
    } else { 
     return false; 
    } 
} 

是什麼真的假&辦? false是否會阻止查詢進一步執行?

其實我已經在這裏我想停止執行,如果用戶沒有在數據庫中找到喜歡的登錄頁面:

if(mysqli_num_rows($query) !=1) { 
    // if result is not 1 then executing must be stop here, hello should not be echo 
} 

echo "hello"; 

進一步下跌是多個腳本,只有當結果爲1

應該執行

http://php.net/manual/en/function.return.php - for those of you who think that using return in a script is the same as using exit note that: using return just exits the execution of the current script, exit the whole execution.

所以我嘗試了代碼在我的本地

$a = 1; 
if($a == 0){ 
echo "Its 0"; } 
else{ return; } 
echo "hi"; 

擰返回false單詞後喜不執行&當我刪除返回false則喜字被執行

現在,我會告訴你我真的想知道

$query3 =mysqli_query($connecDB, "SELECT * FROM users WHERE username='$username'"); 
if(mysqli_num_rows($query3) != 1) 
    { 
     $er1 = "Username doesn't exist"; 
    } 
else{ 
$query4 ="SELECT * FROM users WHERE email='$email'"; 
$result1=mysqli_query($connecDB, $query3); 
if(mysqli_num_rows($result1) != 1) 
    { 
     $er1 = "Email doesn't exist"; 
    } else { 
    // do this 
    } 
    } 

現在你見上面我已經使用,如果語句成其他語句和更多,如果聲明到其他,這使得我的PHP腳本非常複雜&很難理解

我只是想知道什麼是bett呃像圖所示

$query3 =mysqli_query($connecDB, "SELECT * FROM users WHERE username='$username'"); 
    if(mysqli_num_rows($query3) != 1) 
     { 
      $er1 = "Username doesn't exist"; 
     } 
    else{ // stop the execution here & just print this $er1 on the page without exit or die } 

的方式來執行腳本我想停下來,因爲下面的例子

$a = 1; 
if($a == 0){ 
echo "Its 0 "; } 
else{ //if it's not 0 then it means user is not registered do not echo echo hi below } 
echo "hi"; 

現在喜總是執行

+3

這一切都在手冊http://php.net/manual/en/function.return。php –

+0

在你以後的情況下,你可以使用if(check()){然後你有$ _POST ['example']設置並且可以執行一些其他需要這個變量的腳本}。如果您使用DRY編程方法 –

+0

'exit'並且die()會停止執行,那麼使用這些函數總是更好。 –

回答

2

是什麼讓function停止執行是執行控制結構return,truefalseBoolean變量,也分別表示爲10


if(function that returns a boolean !=1) 

如果function that returns a booleantrue1)的if將只執行


瞭解更多關於returnBoolean variables


請注意:由於安全問題,現已棄用,因爲PHP7。建議您切換到mysqli_*PDO擴展名。

+0

如果我使用退出和死亡頁面將退出&空白頁將顯示,什麼可能是最好的方式?目前我正在使用if(mysqli_num_rows($ query)!= 1){} else {echo「hi」} –

+0

你想要做什麼?花點時間思考,更新你的問題將盡可能簡明扼要。 –

+0

請檢查我編輯的問題 –

相關問題