MoMoMajor Posted July 26, 2009 Share Posted July 26, 2009 Is it possible to call a variable from an if statement in a code above the if statement itself? echo $name; if (4 > 3) { $name = 'james'; } That variable $name will be empty because I am calling it before it is created. Is there anyway to pass the $name variable so it can be used anywhere in the scrpt? Quote Link to comment https://forums.phpfreaks.com/topic/167441-getting-variables-back-from-if-statement/ Share on other sites More sharing options...
.josh Posted July 26, 2009 Share Posted July 26, 2009 no...script is parsed top to bottom, left to right. You have to assign something to it before you can use it. The only sort of exception is calling a function or class before defining it. Example: doSomething(); function doSomething() { echo "something"; } Quote Link to comment https://forums.phpfreaks.com/topic/167441-getting-variables-back-from-if-statement/#findComment-882919 Share on other sites More sharing options...
MoMoMajor Posted July 26, 2009 Author Share Posted July 26, 2009 no...script is parsed top to bottom, left to right. You have to assign something to it before you can use it. The only sort of exception is calling a function or class before defining it. Example: doSomething(); function doSomething() { echo "something"; } Ok what I want to do is get information from the $error variables after a form is submitted. This is my code if(( ($errors == 0) && (isset($_POST['submit_image']))){ header("location: index.php"); unset($_FILE['userfile']); } followed by <?php ######################################################################################################################################################################## if(isset($_POST['submit_image'])) { //see if submit button is pressed. //Check image code here } else { //display form ?> <form enctype="multipart/form-data" action="#" method="post" name="uploadImage" /> <fieldset class="image_uploader"> <legend class="headers"><strong>Image Upload</strong></legend><br /> <input type = "hidden" name="MAX_UPLOAD_SIZE" value="500000" /> <p class="image_form_namer">Name the file:</p> <input class="middle_input" type="text" name="image_name" size="35" /><br/><br/> <p class="image_form_blurb">Add a short description(optional):</p><input type="text" value="" name="image_description" size="140" maxlength="150"/> <input type="file" name="userfile" size="35" /><br/> <input type="submit" name="submit_image" value="Upload Image"> </fieldset> <?php } //end else ?> <?php //check if they decided to upload a pic: //if($_FILES['userfile']['size'] > 1) { if (isset($_POST['submit_image'])) { // set some variables for the maximum parameters $max_size = 700000; $max_height = 800; $max_width = 800; $error_message = array(); if(($_FILES['userfile']['size'] > $_POST['MAX_UPLOAD_SIZE']) || ($_FILES['userfile']['size'] > $max_size)) { $error_message['size'] = "Upload file size too large: (<b>{$_FILES['userfile']['size']}</b>). Must not exceed {$max_size}kb."; } $array = explode(".", $_FILES['userfile']['name']); $nr = count($array); $ext = $array[$nr-1]; // I condensed all 3 of the next if's into one... since they were checking for the same thing pretty much. if(($ext !="jpg") && ($ext !="jpeg") && ($ext !="png") && ($ext !="pjpeg") && ($_FILES['userfile']['type'] != "image/jpeg") && ($_FILES['userfile']['type'] != "image/pjpeg") && ($_FILES['userfile']['type'] != "image/png") && ($info['mime'] != "image/jpeg") && ($info['mime'] != "image/pjpeg") && ($info['mime'] != "image/png")) { $error_message['format'] = "Upload file type un-recognized. Only .JPG or .PNG images allowed."; } $info = getimagesize($_FILES['userfile']['tmp_name']); if(($info[0] > $max_width) || ($info[1] >$max_height)) { die("<BR><BR>Error: Image size error (<b>" . $info[0] . "</b> x <b>" . $info[1] . "</b>). Must not exceed ". $max_height . " x ". $max_width ."."); $error_message['widthnheight'] = "Upload file type un-recognized. Only .JPG or .PNG images allowed."; } if(count($error_message)>0) { // we had errors: $errors = 1; foreach($error_message as $message) { echo '<strong>ERROR:</strong> '.$message.'<br><br>'; } echo "<br><br><strong>Click here to go back <a href='../".$page_url."/'>Home</a></strong> <br><br>"; die(); } else { $errors = 0; //rename file, move it to location. if(is_uploaded_file($_FILES['userfile']['tmp_name'])) { //generate a random number and letters $randNum = rand(1,2147483647); $int = 5; function randLetter() { $int = rand(0,26); $a_z = "abcdefghijklmnopqrstuvwxyz"; $rand_letter = $a_z[$int]; return $rand_letter; } $string_a = randLetter(); $string_b = randLetter(); $string_c = randLetter(); $short_string = $string_a.$string_b.$string_c; $filename = strtolower($randNum).$short_string; // check if file is uploaded to the specified location and print out a message if(move_uploaded_file($_FILES['userfile']['tmp_name'] , $_SERVER['DOCUMENT_ROOT']."/site/pics/$page_url/".$filename . '.' . $ext)) { echo("File uploaded successfully."); } else { echo("An error occurred while uploading."); }//end upload } //end is_uploaded_file As you can see the $errors variables are within the if statements so I can't return anything from them. All I want to do is be able to tell my script if it should redirect the user with the header command if no errors were found or if it should just display the errors. Quote Link to comment https://forums.phpfreaks.com/topic/167441-getting-variables-back-from-if-statement/#findComment-882927 Share on other sites More sharing options...
.josh Posted July 26, 2009 Share Posted July 26, 2009 so you need to reorder your code. Do all the condition code that generates the error messages, then do the condition that checks if error messages exist. Quote Link to comment https://forums.phpfreaks.com/topic/167441-getting-variables-back-from-if-statement/#findComment-882929 Share on other sites More sharing options...
MoMoMajor Posted July 26, 2009 Author Share Posted July 26, 2009 so you need to reorder your code. Do all the condition code that generates the error messages, then do the condition that checks if error messages exist. Thanks for your reply. Can you please explan a little more on how this would affect the first if statement that checks for errors? I am new to this and can't get my head around why the $errors variable is empty. I know it would be empty before the form is submitted but why would it be empty after submission? if(( ($errors == 0) && (isset($_POST['submit_image']))){ header("location: index.php"); unset($_FILE['userfile']); } After the form is submitted wouldn't the $errors variable either contain a 1 or 0? Quote Link to comment https://forums.phpfreaks.com/topic/167441-getting-variables-back-from-if-statement/#findComment-882935 Share on other sites More sharing options...
.josh Posted July 26, 2009 Share Posted July 26, 2009 No. http is a stateless protocol. What that means is once a script is run, as far as the server is concerned, you no longer exist. So when you submit a form, anything generated from the previous script execution doesn't exist. Now, you can use things like cookies and session variables and flatfiles and databases to store information and retrieve it from one page to another, but you aren't doing any of that. Nor do you really need to, in this instance. You just need to reorder the flow of your script. Your script order should be as such (pseudo code): if (isset($_POST)) { // if there is something in the post array, that means a form was submitted // validate info, process submitted data, generate error variable(s) if things don't check out if ($errors == 0) { // after all that stuff above, if no errors were generated... // do header stuff here } } // if the script makes it this far, that means either form was not post or else there were errors and there was no header redirect. // echo out error messages if any // echo out form edit: I simplified it a bit Quote Link to comment https://forums.phpfreaks.com/topic/167441-getting-variables-back-from-if-statement/#findComment-882940 Share on other sites More sharing options...
MoMoMajor Posted July 26, 2009 Author Share Posted July 26, 2009 No. http is a stateless protocol. What that means is once a script is run, as far as the server is concerned, you no longer exist. So when you submit a form, anything generated from the previous script execution doesn't exist. Now, you can use things like cookies and session variables and flatfiles and databases to store information and retrieve it from one page to another, but you aren't doing any of that. Nor do you really need to, in this instance. You just need to reorder the flow of your script. Your script order should be as such (pseudo code): if (isset($_POST)) { // if there is something in the post array, that means a form was submitted // validate info, process submitted data, generate error variable(s) if things don't check out if ($errors == 0) { // after all that stuff above, if no errors were generated... // do header stuff here } } // if the script makes it this far, that means either form was not post or else there were errors and there was no header redirect. // echo out error messages if any // echo out form edit: I simplified it a bit I won't be able to use the code above because I would certainly get a header error back because of the divs and other html tags before the image upload form. I see where you are coming from though and I see where I was wrong all this time. I am not sure where to go from here because moving all the html around would certainly ruin my page layout. Quote Link to comment https://forums.phpfreaks.com/topic/167441-getting-variables-back-from-if-statement/#findComment-882947 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.