Jump to content

[SOLVED] Echoing before if


grahamb314

Recommended Posts

Hi all

I have an if statement which adds 1 to a variable.

 

On my page I want it to be echoed at the top of the page. but I have several if's, echoing different things in different places.

 

Is there any way I can get it to echo before the if (And take into account what the if does to it)?

 

Thanks

Link to comment
Share on other sites

Here is an example:

 

$englishnumber = null
if ($number = 0)
{$englishnumber = zero}

echo $englishnumber;

 

I want to echo the englishnumber before the if:

 

echo $englishnumber; // I want this to echo "zero"

$englishnumber = null
if ($number = 0)
{$englishnumber = zero}


Is there some special variable that can do this?

 

Link to comment
Share on other sites

You might be looking for a shorthand notation its basicly puts the ifelse into the echo

 

<?php
$englishnumber=0;
echo($englishnumber==0 || $englishnumber==NULL)?"zero":$englishnumber;
?>

 

so I could do:

 

<?php
$englishnumber=0;
echo($englishnumber>0 || $englishnumber==NULL)?"There are one or more errors":$englishnumber;
?>

Is this like saying if($nooferrors>0)

{echo "there are one or more errors";)

 

The problem is that I have serveral if's which add one to the variable if there is an error  (Each if checks for a different error)

 

 

Link to comment
Share on other sites

It's impossible.

 

So you want to return the value of a variable that is set in an if statement BEFORE the if statement runs?

 

Do you see how this is impossible?

 

The echo has to come after the if statement...after the variable has been set.

 

Best,

 

Damien.

Link to comment
Share on other sites

i want a php page that just says ERROR! at the top is there is an error,

 

there is an error when the error variable is > 0

 

The error variable is set with a few if's :

if condiiton 1 is true then $numberoferrors = $numberoferrors + 1

 

if condiiton 2 is true then $numberoferrors = $numberoferrors + 1

 

if condiiton 3 is true then $numberoferrors = $numberoferrors + 1

 

 

etc etc

 

This page has some other echoing on it like echoing if the data has been sent to the DB,

the data can only be sent if $numberoferrors = 0

(This code is at the bottom of the page)

 

I want a big red thing on the top of the page to say THERE ARE ERRORS! if $numberoferrors >0 but the if's which set the variable are near the bottom of the page.

 

its a tricky one, I understand

 

 

Link to comment
Share on other sites

doesn't sound tricky at all.  It seems you just want to validate some kind of data and if it passes it does something otherwise it shows an error. But what is it you want to validate?

 

Well the data is posted from a form on another page.

the if works fine)It checks to see if the text is less than 50 characters) if it doesnt then it makes the error variable +1

i just want to echo the variable at the top of the page when it is >0

Link to comment
Share on other sites

My validation works fine though.

All I want is for an error message to be displayed on the top of the page - Maybe this is more of a Javascript thing?

 

Thanks for your help anyways

I've decided to more the if statements to the top of my code and echo an error under them

 

thanks again,

 

 

Link to comment
Share on other sites

I'm thinking maybe this is what you're looking for:

 


if (something blah something){ $error = $error +1;}
if (something blah something){ $error = $error +1;}
if (something blah something){ $error = $error +1;}

 

Then, I'm guessing you want to count the errors and echo how many?

 

echo "$error";

 

Your issue is that, like you said, the ifs are at the bottom. Well, they can't be at the bottom. They have to be right after the form input.

 

Like so:

 


$varfromform = $_POST['inputfromform'];

$error = 0;
if($varfromform ==""){$error = $error +1;}

if($error > 0){"ERRORS!"; [b]return;[/b]}

That's it. If there are errors, no more of the script will work.

It's so much tricky as badly implemented. Why must the ifs be right at the bottom?

Best Regards,

Damien.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.