If else Variables

Hi there i have a form input called ADDRESS

when i retrieve data from database i am showing this variable on a page.

At the moment my code is
echo “” .$row[“ADDRESS”]. “

”;

what do i need to do to add a message so that if the ADDRRESS is not filled in it echo;s No Address submitted ?

I know its an if else statement but not sure of coding.

thanks

Well, if you mean to check if it is empty or null or blank? Lots of ways to check that.
Normally if you enter it into a database, you can check if it exists or is empty. Something like this would work for you;

if (isset($row["ADDRESS"]) AND trim($row["ADDRESS"])!="") {
   //  There is something in the address field
   echo $row["ADDRESS"];
} else {
   //  Nothing there...
   echo "You need to complete the address field!";
}

You can also use === to handle this in an IF clause, but, this way seems to work good enough.

But, it would be much better to make sure the field is not empty before you save it to the database.
Normally, when you enter data, you need to validate all the inputs to make sure they are correct.
THEN, save them to the database. In this way, you would never need to check things when displaying them. Make sense?

1 Like

That is brilliant thank you so much i did try if else statement but must have got code wrong somewhere.
Much Appreciated

Would something like this work aswell ?

<?php

echo $row["ADDRESS"] ?? "You need to complete the address field!";

Yes and no. It is much much harder to understand what that means. It also does not give you two easy to use and handle options. It also depends on how you would use that. Lots of small issues with that shortcut.

Shortcuts are great for experienced programmers where others will never need to understand their code.
But, for beginners and others, shortcut code is often too hard to understand for others.

1 Like

Alright. Thanks for your answer.

Sponsor our Newsletter | Privacy Policy | Terms of Service