Google Answers Logo
View Question
 
Q: PHP coding ( No Answer,   4 Comments )
Question  
Subject: PHP coding
Category: Computers > Programming
Asked by: vladuz-ga
List Price: $5.00
Posted: 03 Aug 2004 18:34 PDT
Expires: 03 Aug 2004 20:31 PDT
Question ID: 383184
Hello.
My question is about PHP, a simple question.
Let's say I have $number, where number = xxxxxx.
If starts with 1234, echo 'a'
If it doesn't start with 1234, echo 'b'.
Substr, yes, but code?
Answer  
There is no answer at this time.

Comments  
Subject: Re: PHP coding
From: mike_sysadmin-ga on 03 Aug 2004 18:57 PDT
 
Hi vladuz,

That's an easy one. Here's a snippet:

<?

// We create a function that checks the numbers
function checkNumber($numberToCheck) {
  if (substr($numberToCheck, 0, 3) == "1234") echo "a"; else echo "b";
}

// We asign values to some variables
$number = 1234567678;
$otherNumber= 9876545;

// We check them if they start with 1234 using the function above.
checkNumber($number);
checkNumber($otherNumber);

?>

The first check should return "a". The second one should return "b"

- Mike
Subject: Re: PHP coding
From: crythias-ga on 03 Aug 2004 18:59 PDT
 
From http://www.php.net/manual/en/function.substr.php
<?php
$number="1234567";
$left=substr($searchme, 0,4);
if ($left=="abcd") {
   echo 'a';
   } else {
   echo 'b';
   }
?>
but, if $number is actually an integer, and you will have exactly 6
characters then use round:
http://www.php.net/manual/en/function.round.php
<?php
$number=123456;
if (round($number,-2 )==123400) {
   echo 'a';
   } else {
   echo 'b';
   }
?>
Subject: Re: PHP coding
From: crythias-ga on 03 Aug 2004 19:00 PDT
 
yes, 0,3 duh. not 0,4 :( My bad. :)
Subject: Re: PHP coding
From: vladuz-ga on 03 Aug 2004 19:28 PDT
 
Thanks very much.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy