grahamb314 Posted September 20, 2008 Share Posted September 20, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/125091-solved-echoing-before-if/ Share on other sites More sharing options...
Goldeneye Posted September 20, 2008 Share Posted September 20, 2008 Well providing some code might make it a little easier to help you. Quote Link to comment https://forums.phpfreaks.com/topic/125091-solved-echoing-before-if/#findComment-646494 Share on other sites More sharing options...
Stooney Posted September 20, 2008 Share Posted September 20, 2008 Once you echo something, it's already sent to the browser and cannot be changed. You however use output buffering, but that wouldn't make any sense based on your description. Quote Link to comment https://forums.phpfreaks.com/topic/125091-solved-echoing-before-if/#findComment-646497 Share on other sites More sharing options...
grahamb314 Posted September 20, 2008 Author Share Posted September 20, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/125091-solved-echoing-before-if/#findComment-646498 Share on other sites More sharing options...
Stooney Posted September 20, 2008 Share Posted September 20, 2008 If you want it to echo zero, then it's value must be zero when you echo it. There's no way around that. Quote Link to comment https://forums.phpfreaks.com/topic/125091-solved-echoing-before-if/#findComment-646501 Share on other sites More sharing options...
grahamb314 Posted September 20, 2008 Author Share Posted September 20, 2008 So the only way I can do it, is to move the whole if statement to the top of the page and echo under it? Quote Link to comment https://forums.phpfreaks.com/topic/125091-solved-echoing-before-if/#findComment-646503 Share on other sites More sharing options...
Stooney Posted September 20, 2008 Share Posted September 20, 2008 Based on how little you've really explained, I'm going to say yes. It would help to see what you're trying to do. Quote Link to comment https://forums.phpfreaks.com/topic/125091-solved-echoing-before-if/#findComment-646504 Share on other sites More sharing options...
RichardRotterdam Posted September 20, 2008 Share Posted September 20, 2008 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/125091-solved-echoing-before-if/#findComment-646511 Share on other sites More sharing options...
grahamb314 Posted September 20, 2008 Author Share Posted September 20, 2008 okay, I have an if statement that echos an error message under certain conditions I want the error message to be displayed on the top of the php page but i want to leave the if statement at the bottom of the code Quote Link to comment https://forums.phpfreaks.com/topic/125091-solved-echoing-before-if/#findComment-646515 Share on other sites More sharing options...
grahamb314 Posted September 20, 2008 Author Share Posted September 20, 2008 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) Quote Link to comment https://forums.phpfreaks.com/topic/125091-solved-echoing-before-if/#findComment-646519 Share on other sites More sharing options...
DamienRoche Posted September 20, 2008 Share Posted September 20, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/125091-solved-echoing-before-if/#findComment-646522 Share on other sites More sharing options...
grahamb314 Posted September 20, 2008 Author Share Posted September 20, 2008 I thought it wouldnt be possible, but i thought there might be a special variable or something that might be able to do it. Could I not make another page to hold the variable and then echo it in ? Quote Link to comment https://forums.phpfreaks.com/topic/125091-solved-echoing-before-if/#findComment-646551 Share on other sites More sharing options...
Adam Posted September 20, 2008 Share Posted September 20, 2008 Can echo a variable in any way you like, but unless you've literally set the variable (ie. $var = 'something' .. $var has absoloutley no value, the script cant guess or assume the value... Adam Quote Link to comment https://forums.phpfreaks.com/topic/125091-solved-echoing-before-if/#findComment-646554 Share on other sites More sharing options...
RichardRotterdam Posted September 20, 2008 Share Posted September 20, 2008 It seems to me you are trying to solve a problem that really isnt the main problem. I asume you have a certain situation and you want some kind of result out of it. Can you explain the following things: 1. what is your input 2. what should come out of it/The result Quote Link to comment https://forums.phpfreaks.com/topic/125091-solved-echoing-before-if/#findComment-646555 Share on other sites More sharing options...
grahamb314 Posted September 20, 2008 Author Share Posted September 20, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/125091-solved-echoing-before-if/#findComment-646564 Share on other sites More sharing options...
RichardRotterdam Posted September 20, 2008 Share Posted September 20, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/125091-solved-echoing-before-if/#findComment-646583 Share on other sites More sharing options...
grahamb314 Posted September 20, 2008 Author Share Posted September 20, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/125091-solved-echoing-before-if/#findComment-646584 Share on other sites More sharing options...
RichardRotterdam Posted September 20, 2008 Share Posted September 20, 2008 Then your main problem is form validation. This forum is loaded with form validation questions. you might wanna do a search on "form validation" on this form that will surely help you. otherwise just google up php form validation. if that doesnt help. post some code of your form you want to validate Quote Link to comment https://forums.phpfreaks.com/topic/125091-solved-echoing-before-if/#findComment-646590 Share on other sites More sharing options...
grahamb314 Posted September 20, 2008 Author Share Posted September 20, 2008 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, Quote Link to comment https://forums.phpfreaks.com/topic/125091-solved-echoing-before-if/#findComment-646596 Share on other sites More sharing options...
DamienRoche Posted September 20, 2008 Share Posted September 20, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/125091-solved-echoing-before-if/#findComment-646600 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.