J
J
John2015-10-28 14:32:49
PHP
John, 2015-10-28 14:32:49

How to write if else correctly?

Hello everyone, I have a question how to properly register the if elsecheck!
For example:

<?php
$a = 1;
// проверяем подходит ли переменная
if (empty($a)) {
echo "a = empty";
    exit();
}else{
// если прошло то делаем заключительную проверку   
  if(is_integer($a)){
echo "это число";
    exit();
}else{
echo "нееее";
    exit();
}
}

?>

Is my code correct or is there another way?
PS: the code is just for an example, but I do something similar
in the first condition, I check the text for validity, if the validity is OK, then we do the second condition: check for the presence of text in the database, if the text is found, then write information about the text; if the text is not found, then we write an error , if the validation fails, then we write a validation error. Need to do this!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Kravchenko, 2015-10-28
@mydearfriend

in php there is an elseif (condition) operator, with its help such code will look like this

if(empty($var)){
// code here...
}elseif(is_numeric($var)){
// another code here...
}else{
// and here
}

if(validate_text($text)){
  $text_id_database = $db->select('some query here where text = '.$text);
  if($text_in_database){
    write_some_info();
  }else{
    $errors[] = 'some error';
  }
  
}else{
  $errors[] = 'Invalid text';
}

V
Vladislav Kopylov, 2015-10-28
@kopylov_vlad

$a = 1;
if( empty($a) ){
  echo "a = empty";
elseif(is_integer($a)){
  echo "это число";
}else{
  echo "нееее";
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question