LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2002, 09:13 AM   #1
antken
Member
 
Registered: Nov 2000
Posts: 368

Rep: Reputation: Disabled
IF statements in PHP


hi,

i am writing a simple function in php and it finds a name in a database if there is more than one result it returns a number if the database comes back with no results it returns another number but if it finds only one record then it returns the record number.

so far i have go it to return the number that is on the record in the database, now i have to make it more complex.

in m$ VB they have a IF statement that can work something like the following
Code:
IF foo = bar THEN 
blah

ELSEIF foo = blah THEN
boo

ELSE 
blah

my question is this:
does php's if statement do this as well? or do i have to do it another way?
 
Old 09-16-2002, 12:18 PM   #2
verigoth
Member
 
Registered: May 2002
Posts: 179

Rep: Reputation: Disabled
php uses C syntax:

if(foo==bar)
{
do this;
}
else if(foo==blah)
{
do that;
}
else
{
something else;
}

verigoth
 
Old 09-16-2002, 11:14 PM   #3
ShawnD
Member
 
Registered: Jul 2002
Distribution: Mandrake 8.2
Posts: 127

Rep: Reputation: 15
you do not even need the {} things in PHP
here is a calculator program i made:
http://66.222.164.149/calculator.php
here is the source to it:
PHP Code:
<html>
<head>
<title>calculator program</title>
</head>

<body>
<div align="center">
<form action="<?php echo $PHP_SELF?>" method="POST">
<table width="650" cellspacing="1">
  <tr>
    <td>
      first number
    </td>
    <td>
      operator
    </td>
    <td>
      second number
    </td>
    <td>
      total
    </td>
  </tr>
  <tr>
    <td>
      <input type="text" name="first_number" value="<?php echo $HTTP_POST_VARS['first_number']; ?>">
    </td>
    <td>
      <input type="text" name="operator" value="<?php echo $HTTP_POST_VARS['operator']; ?>">
    </td>
    <td>
      <input type="text" name="second_number" value="<?php echo $HTTP_POST_VARS['second_number']; ?>">
    </td>
    <?php $total=$_GET['total'];
      if(
$HTTP_POST_VARS['operator'] == '+')
        
$total $HTTP_POST_VARS['first_number'] + $HTTP_POST_VARS['second_number'];

      elseif(
$HTTP_POST_VARS['operator'] == '-')
        
$total $HTTP_POST_VARS['first_number'] + $HTTP_POST_VARS['second_number'];

      elseif(
$HTTP_POST_VARS['operator'] == '*')
        
$total $HTTP_POST_VARS['first_number'] * $HTTP_POST_VARS['second_number'];

      elseif(
$HTTP_POST_VARS['operator'] == '/')
        
$total $HTTP_POST_VARS['first_number'] / $HTTP_POST_VARS['second_number'];

      elseif(
$HTTP_POST_VARS['operator'] == '%')
        
$total $HTTP_POST_VARS['first_number'] % $HTTP_POST_VARS['second_number']; ?>
    <td>
      <?php echo("<b>$total</b>"); ?>
    </td>
  </tr>
  <tr>
    <td>
      <input type="submit" name="submit_button" value="process it">
    </td>
  </tr>
</table>
</form>
</body>
</html>
as you can see, it does not have {} things in it yet it still works great
try it out plz, tell me what you think ehehe. it was originaly an assignment from my Practical C++ Programming book but i ported it over to PHP (i hate compiling stuff!!!!)
 
Old 09-17-2002, 08:53 AM   #4
no2nt
Member
 
Registered: Aug 2001
Location: South Carolina, USA
Distribution: Redhat 8.0/Custom
Posts: 96

Rep: Reputation: 16
Word of caution:

Sure, you don't need to use a block {} for single line expressions but it
is not good practice to leave them out. I've seen more than enough problems
during debugging or maintaining code written by junior programmers in our
organization who do not use {}.
 
Old 09-17-2002, 09:02 AM   #5
antken
Member
 
Registered: Nov 2000
Posts: 368

Original Poster
Rep: Reputation: Disabled
i have too done this, i had an if statement saying basically if your browsing our website from the inside it generates a link to an internal machine, but if your outside it links else where.

thing is, cos i missed out the {} i was getting two links.

but thant for mentioning it !


thanks

--antken
_______________________
and dont for get the ;
 
Old 09-17-2002, 09:21 AM   #6
ShawnD
Member
 
Registered: Jul 2002
Distribution: Mandrake 8.2
Posts: 127

Rep: Reputation: 15
you mean if i had something like this?
Code:
if(joe=='happy'){
  echo("joe are so uber l337");
  
  else
    if(joe=='not_happy')
      echo("joe ain't happy");
}
without the {} would it treat this as the second if statement being non tied into the else?
 
Old 09-17-2002, 09:38 AM   #7
antken
Member
 
Registered: Nov 2000
Posts: 368

Original Poster
Rep: Reputation: Disabled
yes,

basically i had what you had above, but after the if statement i had a few other things to do
for example

Code:
if (laa = deedaa) 
     dothis
     do that
     and this

else 
   jump
   eat_the_apple
so above, if laa does equal deedaa then it will process: dothis, do that, and this, and eat_the_apple

but if laa equals something else then php will process: do that, and this, jump, and eat_the_apple
but will not process dothis

including the {} like so:
Code:
if (laa = deedaa) {
     dothis
     do that
     and this
}
else {
   jump
   eat_the_apple
}
then else will process everything inside its brackets and the if will process everything inside its brackets



this was my problem a few days ago i kept forgetting the {} and thus messing my coded up


--antken
_______________________
and dont for get the ;
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
if else statements 0.o Programming 7 09-27-2005 02:59 PM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 02:41 AM
if statements thesnaggle Linux - Software 1 02-16-2004 09:52 AM
PHP: egrep For Loops and If Statements jhrbek Programming 14 12-31-2003 12:26 PM
PHP if statements and variables antken Programming 4 09-24-2003 12:45 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:19 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration