PHP if statement errmsg - php

I have this if statment
if(!empty($URL) && ($safe===true)){
//lots of code
}
Is it possible to show different error messages depending on what condition failed?
For example if $URL is empty echo "URL empty";
and if $safe===false echo "GTFO";

Just add this to your code
else if(empty($URL)
{
echo "url empty";
}
else if($safe===false)
echo "Get Out"; // be polite ;)

if (empty($url))
{
echo "URL empty";
}
elseif ($safe === false)
{
echo "GTFO";
}
else
{
//lots of code
}

} else {
if($safe === false){
die("GTFO");
}
if (empty($url)){
echo "URL Empty.";
}
}

Yes; you could make use of an else if statement.
if (!empty($URL) && ($safe===true)) {
//lots of code
} else if (empty($URL)) {
// report that url is empty
} else if ($safe === false) {
// report that safe is false
}
Alternatively, you could just use an else statement to report that the if condition was false.

I propose the following solution. It will allow you to show multiple errors and set each condition only once (instead of having so many conditions and anti-conditions as other solutions proposed).
$errors = array();
if(empty($URL) {
$errors[] = 'URL empty';
}
if($safe !== true) {
$errors[] = 'GTFO';
}
if(empty($errors)) {
//lots of code
} else {
echo '<ul>';
foreach($errors as $error_message) {
echo '<li>' . $error_message . '</li>';
}
echo '</ul>';
}

Related

IF else & else if in Codeigniter

please check my script.
if($namachief != NULL)
{
echo $namachief;
}
else if ($namarm != NULL)
{
echo $namarm;
}
else
{
echo "Something wrong. Please contact US";
}
My condition isn't working when in this condition => $namarm != NULL, I only get white page but its normal when in this condition => $namachief != NULL.
It's fine when i do this
echo $namachief; & echo $namarm;
if () {
}
else if (){
}
else if(){
------------------------ My Script Here -----------------------------
}
Both answer below are right. My problem lies here. In my form I did this
<option value="none">None</option> Then i change it to <option value="">None</option>
You try like this
if($namachief != NULL || $namachief != "")
{
echo $namachief;
}
else if ($namarm != NULL || $namarm != "")
{
echo $namarm;
}
else
{
echo "Something wrong. Please contact US";
}
Write your condition as below:-
if(isset($namachief) && !empty($namachief)){
echo $namachief;
}
else if (isset($namarm) && !empty($namarm)){
echo $namarm;
}
else{
echo "Something wrong. Please contact US";
}
If you want to print both variables if both are set and have valid values then,
if(empty($namarm) && empty($namachief)){
echo "Something wrong. Please contact US";
}
else if (!empty($namachief) && !empty($namarm)){
echo $namarm;
echo $namachief;
}
else if (isset($namarm) && !empty($namarm)){
echo $namarm;
}
else if(isset($namachief) && !empty($namachief)){
echo $namachief;
}
else{
// else stuff
}

How to store $_FILES superglobal variables in an array and loop through them to find a match in PHP?

I'm running if statements to identify each variable independently. My code is a bit messy and long but it works with no problem. I'd like to see if it is possible to use an array to store them and run a loop to see if there is a match, instead of using if statements.
PHP code...
if (move_uploaded_file($_FILES["index_deslizador_Cfile1"]["tmp_name"], $index_deslC1)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else if (move_uploaded_file($_FILES["index_deslizador_Cfile2"]["tmp_name"], $index_deslC2)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else if (move_uploaded_file($_FILES["index_deslizador_Cfile3"]["tmp_name"], $index_deslC3)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else if (move_uploaded_file($_FILES["index_deslizador_Cfile4"]["tmp_name"], $index_deslC4)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else if (move_uploaded_file($_FILES["index_deslizador_Mfile1"]["tmp_name"], $index_deslM1)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else if (move_uploaded_file($_FILES["index_deslizador_Mfile2"]["tmp_name"], $index_deslM2)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else if (move_uploaded_file($_FILES["index_deslizador_Mfile3"]["tmp_name"], $index_deslM3)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else if (move_uploaded_file($_FILES["index_deslizador_Mfile4"]["tmp_name"], $index_deslM4)) {
echo "<script>window.open('../index.php','_self')</script>";
}
else {
echo "Sorry, there was an error uploading your file.";
}
I'm fairly new to PHP, so I may get scolded for this suggestion :)
If you could get $index_deslC# variables into an array, you might be able to do something like this:
$i = 1
$destination = array($index_deslC...
foreach($_FILES["index_deslizador_Mfile" . $i]["tmp_name"] as $filename) {
isset(move_uploaded_file($filename, $destination[$i]) ?
echo "<script>window.open('../index.php','_self')</script>" : echo '';
$i++;
}

PHP Catch Error

I need to be able to catch an error. I have the following code
if($call['status_id'] != '' && $call['datetime_required'] != '')
{
//do stuff
}
else
{
// tell them how it failed
}
How would I go about displaying the section on which ti failed. So for example I can return a dynamic error message ie
return 'You did not fill in the field $errorField';
Where
$errorField
Is the if check on which it failed.
UPDATE
I currently code like so
if($call['status_id'] != '')
{
if ($call['datetime_required'] != '')
{
//do stuff
}
else
{
// tell them it failed due to the second condition
}
}
else
{
// tell them it failed due to the first condition
}
But would like to do the check in one line and change the message depending on where ti failed.
Note #jack had already posted his answer before this update.
I'm not sure I fully understand you, you mean something like this?
function check($call, $req_fields) {
$failed = array();
foreach($req_fields as $field) {
if(!$call[$field]) {
$failed[] = $field;
}
}
if(count($failed)) {
return join(', ', $failed) . ' are required.';
}
return 'success?' ;
}
$message = check($call, array('status_id', 'datetime_required'));
if($call['status_id'] != '')
{
if ($call['datetime_required'] != '')
//do stuff
}
else
{
// tell them it failed due to the second condition
}
}
else
{
// tell them it failed due to the first condition
}

Add or to an if condition in PHP

I have this if condition:
if (isset($_REQUEST['altgeraet'])) {
$Altgeraet = 'OK';
} else {
$Altgeraet = 'NOK';
}
And I want when in the SQL Table Host_alt the value "KeinAlterHost" is the
$Altgeraet = 'OK'
This is what I tried but it didn't work:
if (isset($_REQUEST['altgeraet'])
OR ($resultarray['Hostname_alt'] == "KeinAlterHost")) {
$Altgeraet = 'OK';
} else {
$Altgeraet = 'NOK';
}
So is this setup right? I used the array_key_exists
if ((isset($_REQUEST['altgeraet']) OR (array_key_exists('KeinAlterHost',$resultarray['Hostname_alt'])) {
$Altgeraet = 'OK';
} else {
$Altgeraet = 'NOK';
}
You need an pipeline for OR
So the code will be this:
if (isset($_REQUEST['altgeraet']) || ($resultarray['Hostname_alt'] == "KeinAlterHost"))
The isset isn't quit right...
if (isset($_REQUEST['altgeraet']) OR ($resultarray['Hostname_alt'] == "KeinAlterHost")) {
$Altgeraet = 'OK';
} else {
$Altgeraet = 'NOK';
}
You need to quit it earlyer:
if (isset($_REQUEST['altgeraet']) OR ($resultarray['Hostname_alt'] == "KeinAlterHost")) {
$Altgeraet = 'OK';
} else {
$Altgeraet = 'NOK';
}
Because it checks if the request isset, and the hostname isset at you're old code. by the new one, it checks if the request isset, if not check if hostname == keinalterhost and then do the stuff...

PHP File Input Validation

How can I create a function in PHP that checks if a file input is selected? The following is what I have but its not working.
Thank you!
function mcFileUp($mcFile){
if(empty($_FILES[$mcFile]['name'])){
echo 'empty';
}
else{
echo 'Ok!';
}
}
I figured it out:
function mcFileUp($mcFile){
if(empty($mcFile))
{
echo 'empty';
}
else
{
echo 'Ok!';
}
}
mcFileUp($_FILES['mcFileUpload']['name']);
Here is a possible solution
function mcFileUp($mcFile)
{
if(strlen($_FILES[$mcFile]['name'])==0)
{
echo 'empty';
}
else
{
echo 'Ok!';
}
}
if(isset($_FILES["uploaded_file"]))
{
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0))
{
............
}
else
{
echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
}
}
else
{
echo "Error: No file uploaded";
}
Check THIS page if you want to know props and corns about the superglobal array "$_FILE". Also, you can have a look # THIS tutorial. Hope it helps you...

Categories