enthused_confused Posted September 2, 2017 Share Posted September 2, 2017 Hello Freaks. Can someone please tell me why $Phone is null and how to get it to contain the value of $resultPhone as it echoed in the success statement ? I thought if I use a return $var; such as I have used return $resultPhone; the value of that $var was stored for further use. I suspect this is truly simple task to accomplish, but it completely has me confused at this point. <?php $errors = array(); if(isset($_POST['submit'])){ //echo 'Submit button pressed!'.'<br>'; //print_r($_POST); $postedPhone = null; if($_POST['req-phone']){ $postedPhone = $_POST['req-phone']; $postedPhone = trim(stripslashes($postedPhone)); var_dump($postedPhone.'<br>'); } if(strlen($postedPhone) === 0){ //Blank string, add error to $errors array. $errors[] = "You must enter your phone number!"; } if(strlen($postedPhone)<10){ // Must be at least 10 characters,probably forgot to enter area code $errors[] = "Please enter a 10 digit phone number "; } if(strlen($postedPhone)>20){ $errors[] = "Your entered phone number is too long."; } $phonedata = $postedPhone; $resultPhone = null; function format_phone_number($phonedata){ if( preg_match( '/^\+?\d?\-?\(?(\d{3})\/?\)?\-?(\d{3})\/?\-?(\d{4})$/', $phonedata, $matches ) ) $resultPhone = $matches[1] . '-' .$matches[2] . '-' . $matches[3]; if(strlen($resultPhone <10)){ $errors[] = "You have entered an invalid phone number"; }else{ echo '<span style="color:green;">Success</span>'.' '.'<b>'.$resultPhone.'</b>'.' '.'<span style="color:green;">is a valid phone number.</span>'.'<br>'; return $resultPhone; var_dump($resultPhone.'<br>'); } } var_dump($resultPhone.'<br>'); format_phone_number($phonedata); // outputed format is 3 digit-3 digit-4digit like this: 123-456-7890 var_dump($resultPhone.'<br>'); $Phone = $resultPhone; if(!empty($errors)){ echo '<h1>Error(s)!</h1>'; foreach($errors as $errorMessage){ echo '<span style="color:red;">'.$errorMessage .'</span>'; } } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <!--<link rel="stylesheet" href="test_case_style.css" type="text/css">--> </head> <body> <form action="phone_test.php" method="post" id="form1"> <label for="req-phone">Phone*:</label> <input type="text" id="req-phone" name="req-phone" required="required" minlength="10" value="" /> <br> <br> <input type="submit" name="submit" value="submit"/> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/304844-why-phone-is-null/ Share on other sites More sharing options...
Sepodati Posted September 2, 2017 Share Posted September 2, 2017 How does that code even run? There's no opening bracket for your if(preg_match(...)) line or a closing bracket for your function. Quote Link to comment https://forums.phpfreaks.com/topic/304844-why-phone-is-null/#findComment-1550616 Share on other sites More sharing options...
enthused_confused Posted September 2, 2017 Author Share Posted September 2, 2017 Am I missing something here? The line of code with preg_match has four (4) parenthesis "(preg_match(...$matches)) Sorry about the extra white spaces forgot to eliminate them before posting. The closing bracket for the function is two (2) lines above format_phone_number($phonedata); Have you tried to run the code? It successfully formats the phone number and echos the formatted number in the success message. Quote Link to comment https://forums.phpfreaks.com/topic/304844-why-phone-is-null/#findComment-1550618 Share on other sites More sharing options...
Solution Barand Posted September 2, 2017 Solution Share Posted September 2, 2017 It's your formatting and inconsistent indentation that is causing the confusion. As for your problem, you return the number from the function but throw it away. You need to assign the result of the function call to a variable EG $resultPhone = format_phone_number($phoneData);Functions that return values should not have side effects, such as echoing output. Quote Link to comment https://forums.phpfreaks.com/topic/304844-why-phone-is-null/#findComment-1550621 Share on other sites More sharing options...
enthused_confused Posted September 3, 2017 Author Share Posted September 3, 2017 @Barand Thank you. Your advice was exactly what I needed to correct this issue. Can you please elaborate on the throw it away part. I haven't really grasped that. Is it because I was using the echo or the var_dump? you return the number from the function but throw it away. Quote Link to comment https://forums.phpfreaks.com/topic/304844-why-phone-is-null/#findComment-1550627 Share on other sites More sharing options...
Jacques1 Posted September 3, 2017 Share Posted September 3, 2017 You need to learn the basics of programming. It makes to sense to jump to complex applications when you have no idea how a function works. Pick any programming introduction (does't have to be PHP-related), open the chapter about functions and read up on parameters, return values and scope. Quote Link to comment https://forums.phpfreaks.com/topic/304844-why-phone-is-null/#findComment-1550628 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.