Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Logical Error And Syntax Error

syntax syntax error

  • Please log in to reply
2 replies to this topic

#1 farhanyun91

farhanyun91

    CC Regular

  • Member
  • PipPipPip
  • 46 posts

Posted 13 April 2012 - 12:01 AM

Please anyone, check this out and do tell me what i did wrong


unction add($number1,$number2)
{
$num1=$number1;
global $num1;
$num2=$number2;
global $num2;
return $total;
$total=$num1 + $num2;
}
$total2=add(10,10)

echo 'The result of additional between'.$num1.'and'.$num2.'is'.$total2;
?>

the output from this is Parse error: syntax error, unexpected T_ECHO in C:\xampp\htdocs\Codes\function.php on line

and in my case, line 68 is pertained to echo 'The result of additional between'.$num1.'and'.$num2.'is'.$total2;

i could find anything wrong here.





function test()
{
echo ' My name is Farhan' . ' and I am 21 ';
}

echo 'This is about my self which states there that '.test().'</br>' ;

and the output is: My name is Farhan and I am 21 This is about my self which states there that
how can i make it This is about my self which states there that My name is Farhan and I am 21?

what did i do wrong?
  • 0

#2 bbqroast

bbqroast

    Codecall Addict

  • Senior Member
  • PipPipPipPipPipPip
  • 564 posts
  • Location:/etc/passwd

Posted 13 April 2012 - 12:44 AM

Please use the code tags when posting code (the <> icon on the lower styling bar).
It seems you have a type in function (I have never heard of a unction) and are missing a semi colon (a ;) to end the add() function.
Anyway:
<?php
function add($number1,$number2)
{
$num1=$number1;
global $num1;
$num2=$number2;
global $num2;
return $total;
$total=$num1 + $num2;
}
$total2=add(10,10);
echo 'The result of additional between'.$num1.'and'.$num2.'is'.$total2;
?>
Try that.

EDIT: Holy lord of Notch! I have five hundred and thirty two posts! I didn't even know I had more than two hundred :D.
  • 0
Please, write clearly with proper structure. Double spacing makes the text feel un-jointed, Capitalizing Every Word Means People Stop Before Every Word Sub-Consciously Which Is A Pain In The Backside, and use code tags! (The right most styling box).

#3 papabear

papabear

    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 472 posts
  • Location:DarkSide

Posted 13 April 2012 - 12:51 AM

let's fix the easy one first


function test()
{
echo ' My name is Farhan' . ' and I am 21 ';
}

echo 'This is about my self which states there that '.test().'</br>' ;

instead of using echo in the function to display "My name is Farhan and I am 21", use return instead something like this


function test()[/size]
{
return ' My name is Farhan' . ' and I am 21 ';
}

echo 'This is about my self which states there that '.test().'</br>' ;


let's jump into the other one

function add($number1,$number2)
{
$num1=$number1;
global $num1;
$num2=$number2;
global $num2;
return $total;
$total=$num1 + $num2;
}
$total2=add(10,10)

echo 'The result of additional between'.$num1.'and'.$num2.'is'.$total2;
?>

first you have to initialize the global variable first before assigning a value into them.
Second remember to put the return statement at the very end of the code always, except if there is a control statement like IF/ELSE
because when a return statement was triggered the program will ignore the operation below that return statement and forgot to add a semicolon into your $total2 line :)

try this one

function add($number1, $number2) {
	global $num1;
	global $num2;
	$num1 = $number1;
	$num2 = $number2;
	$total = $num1 + $num2;
	return $total;
}

$total2 = add(10,10);

echo 'The result of additional between'.$num1.'and'.$num2.'is'.$total2;

and here's a shorter code for you :)

function add($number1, $number2) {
	 $total = $number1+ $number2;
	 return 'The result of additional between'.$number1.'and'.$number2.'is'.$total;
}

echo add(10,10);

  • 0
Life has no CTRL+Z
Never Forget To HIT "LIKE" If I Helped





Also tagged with one or more of these keywords: syntax, syntax error

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download