Jump to content

Why $Phone is null ?


enthused_confused

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
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
Share on other sites

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
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
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.