Page 1 of 1

New To PHP: Stupid Question...

Posted: Wed Jan 01, 2003 6:20 pm
by Caped Knight
Here's something for you l33t programmers to laugh at. 8O

I just started with PHP a month or so ago. I wanted to start programming, so I started with PHP because I'd heard it was easy and combines style from many other languages. Anyways, I was trying to make a basic calculator-type program to test my skills, but I keep getting the "unexpected T_ELSE on line 17". I really hope there isn't an embarassing number of errors as I think... Here's the code:

Code: Select all

<html>
<head>
<title>??</title>
</head>
<body>

<?

$a = "";          //Empty string for the action form and my variable number 
$b = "5";        //Argument number for $a
$c = $a - $b;

if ($a - $b == "1")
  &#123;
    print "you haev nailed teh number@!!!!!111";
  &#123;
    else ($a - $b !== 1)  //Line 17
     &#123;
       print "You are <b> . $c . </b> off";
     &#125;

?>

</body>
</html>
Can anyone figure this out?

Posted: Wed Jan 01, 2003 6:27 pm
by Beans
You might want to:

1. use a closing bracket "}"instead of an openning one "{" before your else part. Here's a modified code for you:
2. change the type of your variables. $a and $b are string... you can't assign $c to have an integer value (coz $c = $a - $b .. wherein both $a and $b are strings)
3. In your last print statement, notice that you have to close off the quotes when you're going to concatenate.

Code: Select all

<html> 
<head> 
<title>??</title> 
</head> 
<body> 
<? 
$a = 0;          //Empty string for the action form and my variable number 
$b = 5;        //Argument number for $a 
$c = $a - $b; 

if ($c == 1) 
  &#123; 
    print "you have nailed the number@!!!!!111"; 
  &#125;
else ($a - $b !== 1)  //Line 17 
  &#123; 
    print "You are <b>" . $c . "</b> off"; 
  &#125; 
?> 
</body> 
</html>

Posted: Wed Jan 01, 2003 7:07 pm
by Caped Knight
Thanks, Beans. I was able to eliminate syntax errors with by changing the code.

Posted: Wed Jan 01, 2003 9:43 pm
by hob_goblin

Code: Select all

else ($a - $b !== 1)  //Line 17

should just be

Code: Select all

else

Posted: Wed Jan 01, 2003 11:08 pm
by Caped Knight
Heh, yeah, I realised that when I redid the code. I originally had it set to an elseif but I forgot to remove the argument.

Posted: Thu Jan 02, 2003 2:41 am
by Beans
:? slipped my modification.. hahaha