Jump to content

Why $Phone is null ?


Go to solution Solved by Barand,

Recommended Posts

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>

 

Link to comment
https://forums.phpfreaks.com/topic/304844-why-phone-is-null/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/304844-why-phone-is-null/#findComment-1550618
Share on other sites

  • Solution

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.
Link to comment
https://forums.phpfreaks.com/topic/304844-why-phone-is-null/#findComment-1550621
Share on other sites

@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. 

 

Link to comment
https://forums.phpfreaks.com/topic/304844-why-phone-is-null/#findComment-1550627
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/304844-why-phone-is-null/#findComment-1550628
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.