Loops in php

Can’t understand the use of loops. Everyone shows examples with numbers, this is understandable, even there were no problems, but what about information? Here, for example, as I understand it, there is a condition if(!$query){etc. else{then again if, I can’t understand the sequence or how to explain it correctly

<?php
function GetDataArt($art_id)
{
    $sel   = "SELECT `title`,`page_title`,`meta_d`,`meta_kw`,`content` FROM `articles` WHERE `id` = '$art_id' LIMIT 1";
    $query = mysql_query($sel);
    if (!$query) {
        echo ('     !');
    } else {
        if (mysql_num_rows($query) > 0) {
            $res        = mysql_fetch_array($query);
            $title      = $res['title'];
            $page_title = $res['page_title'];
            $meta_kw    = $res['meta_kw'];
            $meta_d     = $res['meta_d'];
            $content    = $res['content'];
        } else {
            $title      = ' ,      !';
            $page_title = ' ,      !';
            $meta_kw    = $meta_d = $content = '';
        }
        $data_arr = array(
            $title,
            $page_title,
            $meta_kw,
            $meta_d,
            $content
        );
        return $data_arr;
    }
}
?>

Answer 1, authority 100%

function GetDataArt($art_id){
   $sel = "SELECT `title`,`page_title`,`meta_d`,`meta_kw`,`content` FROM `articles` WHERE `id` = '$art_id' LIMIT 1";
   $query = mysql_query($sel);
  if(!$query){ //        
    echo('     !');
  }
  else{ //   , - 
    //            -    
    if(mysql_num_rows($query)>0){ //  -      0 ..       - - .
      $res = mysql_fetch_array($query);
      $title = $res['title'];
      $page_title = $res['page_title'];
      $meta_kw = $res['meta_kw'];
      $meta_d = $res['meta_d'];
      $content = $res['content'];
    }
    else{ //             
      $title = ' ,      !';
      $page_title = ' ,      !';
      $meta_kw = $meta_d = $content = '';
    }
    $data_arr = array($title, $page_title, $meta_kw, $meta_d, $content);
    return $data_arr;
  }
}
?>

More questions 🙂

ps and cycles have something to do with it, here is a function and working with a database.

pps

I would rewrite the f-yu a little

function GetDataArt($art_id){
   $data_arr=0;
   $sel = "SELECT `title`,`page_title`,`meta_d`,`meta_kw`,`content` FROM `articles` WHERE `id` = '$art_id' LIMIT 1";
   $query = mysql_query($sel);
  if(!$query){ //        
    echo('     !');
    return $data_arr;  //   -   ,       ""
  }

i.e. the function worked, but in the first case, if the request fails, it will not return anything, therefore, where it will be called, subsequent errors are possible, and I would still check the result of what it returned.


Answer 2, authority 100%

Translate the word if as “if”, and the word else as “else” and read the code, then everything will become clear. You probably don’t mean sequence, but rather nesting.

P.S. If I’m right, then all the superbrains who answered earlier took the question too literally. 🙂 That’s why they are super. 🙂