473,378 Members | 1,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

If Else Statement Wrong

8
Could anyone tell me what's wrong with this code?

Thank you in advance for your effort.

<?php
If ($CategoryName == 'U-100') {
($UnitPrice = 500);
} else if ($CategoryName == 'U-130') {
($UnitPrice = 400);
} else if ($CategoryName == 'U-105') {
($UnitPrice = 350);
} else if ($CategoryName == 'U-95') {
($UnitPrice = 200);
}else if($CategoryName == 'U-85') {
($UnitPrice = 445);
$Total = $ProductUnits*UnitPrice
echo $Total
?>
Dec 1 '06 #1
4 1627
You probably need

($UnitPrice = 445); }

I added the }
Also in this type of situation you might be interested in using a switch/case structure. It's easier to look at (cleaner) and therefore makes it easier to add to in the future, especially by a different person if that is to occur.
http://us2.php.net/manual/en/control...res.switch.php
Note* the ElseIf should work perfectly fine, this is just a consideration
Dec 1 '06 #2
Oh, I forgot something big.
If Else in php is used by elseif , note that it is one word, you used two.
http://us2.php.net/manual/en/control...res.elseif.php
Dec 1 '06 #3
steven
143 100+
Oh, I forgot something big.
If Else in php is used by elseif , note that it is one word, you used two.
http://us2.php.net/manual/en/control...res.elseif.php
Actually both work. There is little difference between the two, however, elseif (the single word) is slightly faster.

The problem is that you're missing the ending curly parenthesis of your if / else statement. I assume it should go after ($UnitPrice = 445);

Also, it is unnecessary to place the variable assignments within brackets.
Dec 1 '06 #4
Actually both work. There is little difference between the two, however, elseif (the single word) is slightly faster.

The problem is that you're missing the ending curly parenthesis of your if / else statement. I assume it should go after ($UnitPrice = 445);

Also, it is unnecessary to place the variable assignments within brackets.
To sum up here is the fixed code:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. If ($CategoryName == 'U-100') {
  3. $UnitPrice = 500;
  4. } elseif ($CategoryName == 'U-130') {
  5. $UnitPrice = 400; 
  6. } elseif ($CategoryName == 'U-105') {
  7. $UnitPrice = 350; 
  8. } elseif ($CategoryName == 'U-95') {
  9. $UnitPrice = 200; 
  10. } elseif($CategoryName == 'U-85') {
  11. $UnitPrice = 445;
  12. }
  13. $Total = $ProductUnits*UnitPrice;
  14. echo $Total;
  15. ?>
Cut and paste as needed.
The alternative, using a case statement would be:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. switch($CategoryName){
  3.  
  4. case 'U-100':
  5. $UnitPrice = 500;
  6. break;
  7.  
  8. case 'U-130':
  9. $UnitPrice = 400;
  10. break;
  11.  
  12. case 'U-105':
  13. $UnitPrice = 350; 
  14. break;
  15.  
  16. case 'U-95':
  17. $UnitPrice = 200; 
  18. break;
  19.  
  20. case 'U-85':
  21. $UnitPrice = 445;
  22. break;
  23.  
  24. }
  25.  
  26. $Total = $ProductUnits*UnitPrice;
  27. echo $Total;
  28. ?>
Hope that helps,
Sean
Dec 1 '06 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: dmbkiwi | last post by:
I am new to this group, and relatively new to python programming, however, have encountered a problem I just cannot solve through reading the documentation, and searching this group on google. I...
5
by: WindAndWaves | last post by:
Hi Team The function below searches all the tables in a database. However, if subsearch = true then it searches all the objects listed in a recordset (which are all table names). I thought to...
18
by: swaroophr | last post by:
Which of switch statement and if-else statement takes less time to execute?
3
by: Amy | last post by:
Hi, I have 6 If Then Else statements I was supposed to write. I did so but I know that they have to be wrong because they all look the same. Could someone take a look at them and point me in the...
13
by: Michael Griebe | last post by:
Simple question. I am optimizing some C++ code and I'd like to know which is faster (or if there is any difference at all) between using a switch statement or nested else-ifs. I'm partial to...
8
by: Jim Michaels | last post by:
C:\prj\quiz\withusers>php tareports.php PHP Parse error: syntax error, unexpected T_ELSE in C:\prj\quiz\withusers\tareports.php on line 205 this is the section of code. if (isset($row4)) {...
2
by: paul | last post by:
Hi all, I've been handed some code and, unless I've got the numbering of parentheses wrong, one of the functions has a curious if-else statement. The thing compiles but is it right? I know the...
20
by: John Salerno | last post by:
I'm starting out with this: try: if int(text) 0: return True else: self.error_message() return False except ValueError: self.error_message()
1
by: RobinAG | last post by:
I'm having a wierd difficulty with my If...ElseIf...Else...End If statement. I'm having the db put together a string to base a recordset off of, and depending on the ID number, the select...
2
by: Speaker | last post by:
I am receiving a few errors when I try to run my program and I can't figure out what I am doing wrong. I'm coding in C++ with minGW on Vista SP1. Here is my code: #include <iostream> using...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.