Will any one please tell me how to run this class. I am getting the FATAL ERROR: Fatal error: Call to undefined function readnumber() in E:\Program Files\xampp\htdocs\numberToWords\numberToWords.php on line 20 while giving input as 120
<?php
class Test
{
function readnumber($num, $depth)
{
$num = (int)$num;
$retval ="";
if ($num < 0) // if it's any other negative, just flip it and call again
return "negative " + readnumber(-$num, 0);
if ($num > 99) // 100 and above
{
if ($num > 999) // 1000 and higher
$retval .= readnumber($num/1000, $depth+3);
$num %= 1000; // now we just need the last three digits
if ($num > 99) // as long as the first digit is not zero
$retval .= readnumber($num/100, 2)." hundred\n";
$retval .=readnumber($num%100, 1); // our last two digits
}
else // from 0 to 99
{
$mod = floor($num / 10);
if ($mod == 0) // ones place
{
if ($num == 1) $retval.="one";
else if ($num == 2) $retval.="two";
else if ($num == 3) $retval.="three";
else if ($num == 4) $retval.="four";
else if ($num == 5) $retval.="five";
else if ($num == 6) $retval.="six";
else if ($num == 7) $retval.="seven";
else if ($num == 8) $retval.="eight";
else if ($num == 9) $retval.="nine";
}
else if ($mod == 1) // if there's a one in the ten's place
{
if ($num == 10) $retval.="ten";
else if ($num == 11) $retval.="eleven";
else if ($num == 12) $retval.="twelve";
else if ($num == 13) $retval.="thirteen";
else if ($num == 14) $retval.="fourteen";
else if ($num == 15) $retval.="fifteen";
else if ($num == 16) $retval.="sixteen";
else if ($num == 17) $retval.="seventeen";
else if ($num == 18) $retval.="eighteen";
else if ($num == 19) $retval.="nineteen";
}
else // if there's a different number in the ten's place
{
if ($mod == 2) $retval.="twenty ";
else if ($mod == 3) $retval.="thirty ";
else if ($mod == 4) $retval.="forty ";
else if ($mod == 5) $retval.="fifty ";
else if ($mod == 6) $retval.="sixty ";
else if ($mod == 7) $retval.="seventy ";
else if ($mod == 8) $retval.="eighty ";
else if ($mod == 9) $retval.="ninety ";
if (($num % 10) != 0)
{
$retval = rtrim($retval); //get rid of space at end
$retval .= "-";
}
$retval.=readnumber($num % 10, 0);
}
}
if ($num != 0)
{
if ($depth == 3)
$retval.=" thousand\n";
else if ($depth == 6)
$retval.=" million\n";
if ($depth == 9)
$retval.=" billion\n";
}
return $retval;
}
}
$objTest = new Test();
$objTest->readnumber(120,0);
?>
You are using the readnumber function in the class itself, try this instead where it appears $this->readnumber
Related
I have a for loop to cycle through and array, run a database query in relation to each element, then call a function that prints out something in relation to it. The array is 12 elements long but the for loop never gets past element 0. It doesn't error or fail it just doesn't do anything after the first element. I verified that by putting the echo $x; and echo $vendorsname[$x]; at the start of each loop cycle and sure enough it only ever echo's 0 out to the page.
$continuetill = count($vendorsname);
for ($x = 0; $x < $continuetill; $x++)
{
echo $x;
echo $vendorsname[$x];
$sql="SELECT low,mid,high,verlow,vermin,verhigh FROM vendors WHERE vendor = ".$x." ORDER BY id DESC LIMIT 1";
if ($result=mysqli_query($conn,$sql))
{
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
$low = $row[0];
$mid = $row[1];
$high = $row[2];
$verlow = $row[3];
$vermid = $row[4];
$verhigh = $row[5];
if(($low > $mid) && ($low > $high))
{
likely295Message($vendorsname[$x]);
}
elseif (($high > $low) && ($high > $mid) && ($high < 15))
{
possibly300Message($vendorsname[$x]);
}
elseif (($high > $low) && ($high > $mid) && ($high >= 15))
{
likely300Message($vendorsname[$x]);
}
elseif (($mid > $low) && ($mid > $high))
{
likely296Message($vendorsname[$x]);
}else
{
unknownMessage($vendorsname[$x]);
}
if(($verlow != 0) || ($vermid != 0) || ($verhigh != 0))
{
if(($verlow > $vermid) && ($verlow > $verhigh))
{
verified295Message($vendorsname[$x]);
changeBackgroundBack($vendorsname[$x]);
changeImage($vendorsname[$x]);
}
elseif (($verhigh > $verlow) && ($verhigh > $vermid))
{
verified300($vendorsname[$x]);
changeBackground($vendorsname[$x]);
changeImage($vendorsname[$x]);
}
elseif (($vermid > $verlow) && ($vermid > $verhigh))
{
verified296($vendorsname[$x]);
changeBackgroundBack($vendorsname[$x]);
changeImage($vendorsname[$x]);
}
}
}
mysqli_free_result($result);
}
}
Make sure you have error displaying turned on. Add at the beginning of your script:
ini_set('display_errors', 1);
to make sure you don't have any errors.
I have written a PHP function to add the appropriate stems to each rank. I.E. 1st 2nd 3rd... so on and so forth.
When $num = 0 the displayed result is "0th", is there a way to display this a 'No Data' instead?
function ordinalSuffix($num) {
$suffixes = array("st", "nd", "rd");
$lastDigit = $num % 10;
if(($num < 20 && $num > 9) || $lastDigit == 0 || $lastDigit > 3) return "th";
return $suffixes[$lastDigit - 1];
}
Like this?
function ordinalSuffix($num) {
//Check if $num is equal to 0
if($num == 0){
//return
return 'No Data';
}
$suffixes = array("st", "nd", "rd");
$lastDigit = $num % 10;
if(($num < 20 && $num > 9) || $lastDigit == 0 || $lastDigit > 3) return "th";
return $suffixes[$lastDigit - 1];
}
function ordinalSuffix($num) {
$suffixes = array("st", "nd", "rd");
$lastDigit = $num % 10;
if(($num < 20 && $num > 9) || $lastDigit == 0 || $lastDigit > 3)
{
return "th";
}
elseif($num == "0")
{
return "no data";
}
return $suffixes[$lastDigit - 1];
}
This does not add to your function but I thought it would be fun to post a one-line solution anyways.
function ordinalSuffix($n) {
return ($n==0?'No Data':date('S',mktime(0,0,0,1,($n%10==0?9:($n%100>20?$n%10:$n%100)),2000)));
}
My timestamp code worked well before i put it in a function.. Get this parse error on line 55, ive marked the line number with "// LINE 55", cant figure it out :(
Here's my error:
Parse error: syntax error, unexpected '.' in C:\wamp\www\flueforumdk\config.php on line 55
Here's the calling of the function:
$GET_UNIX_STAMP_FROM_DB = $art[tidspunkt];
$UNIX_TIME_SECONDS = $GET_UNIX_STAMP_FROM_DB;
echo timestamp_converter($UNIX_TIME_SECONDS);
Here's my function code:
## TIMESTAMP CONVERTER FUNCTION
function timestamp_converter($UNIX_TIME_SECONDS){
// UDREGNING FRA UNIX TIME
$tid = time() - $UNIX_TIME_SECONDS;
$timer = floor($tid/3600);
$minutter = floor($tid/60);
$dage = floor($timer / 24);
$uge = floor($dage / 7);
$month = floor($dage / 30.5);
$aar = floor($dage / 365);
if($tid < 60){
echo"<b>$tid</b> sekunder";
} elseif ($tid > 60){
echo"";
}
if($minutter == 0){
echo"";
} elseif ($minutter < 60){
if($minutter == 1){
echo"<b>$minutter</b> minut";
}else{
echo"<b>$minutter</b> minutter";
}
}
if($timer == 0){
echo"";
} elseif ($timer < 24){
if($timer == 1){
echo"<b>$timer</b> time";
}else{
echo"<b>$timer</b> timer";
}
}
//LINE 55 if($dage == 0){
echo"";
} elseif ($dage < 7){
if($dage == 1){
echo"<b>$dage</b> dag";
}else{
echo"<b>$dage</b> dage";
}
}
if($uge == 0){
echo"";
} elseif ($uge < 4){
if($uge == 1){
echo"<b>$uge</b> uge";
}else{
echo"<b>$uge</b> uger";
}
}
if($month == 0){
echo"";
} elseif ($month < 12){
if($month == 1){
echo"<b>$month</b> måned";
}else{
echo"<b>$month</b> måneder";
}
}
if($aar == 0){
echo"";
} elseif ($aar > 0){
if($aar == 1){
echo"<b>$aar</b> år";
}else{
echo"<b>$aar</b> år";
}
}
}
I'm new to PHP and trying to create the following whilst minimizing the amount of code needed. PHP should show a list of 100 then display if the number is / by 3, 5 or 3 and 5. If not by any then show nothing.
This is what I've got so far, but any help would be great since not sure about the / by 3 and 5 bit as you can see below.
<?php $var = range(0, 100); ?>
<table>
<?php foreach ($var as &$number) {
echo " <tr>
<td>$number</td>
<td>";
if($number % 3 == 0) {
echo "BY3";
} elseif ($number % 5 == 0) {
echo "BY5";
} elseif ($number % 3 and 5 == 0) {
echo "BY3 AND 5";
}
echo "</td></tr>";
}
?>
</table>
Thanks
Nope... you should check first if it's divisble for 15 (3x5) (or 3 and 5) and after you can do other checks:
if($number % 15 == 0) {
echo "BY3 AND 5";
} elseif ($number % 5 == 0) {
echo "BY5";
} elseif ($number % 3 == 0) {
echo "BY3";
}
echo "</td></tr>";
?>
Because every number divisble for 15 is also divisble for 3 and 5. So your last check could never hit
if I'm reading your question correct then you are looking for :
if ($number % 3 == 0 && $number %5 == 0) {
echo "BY3 AND 5";
} elseif ($number % 3 == 0) {
echo "BY3";
} elseif ($number % 5 == 0) {
echo "BY5";
}
Alternative version :
echo ($number % 3 ? ($number % 5 ? "BY3 and 5" : "BY 3") : ($number % 5 ? "BY 5" : ""));
$num_count = 100;
$div_3 = "Divisible by 3";
$div_5 = "Divisible by 5";
$div_both = "Divisible by 3 and 5";
$not_div = "Not Divisible by 3 or 5";
for($i=0;$i<=$num_count;$i++)
{
switch($i)
{
case ($i%15==0):
echo $i." (".$div_both.")</br>";
break;
case ($i%3==0):
echo $i." (".$div_3.")</br>";
break;
case ($i%5==0):
echo $i." (".$div_5.")</br>";
break;
default:
echo $i."</br>";
break;
}
}
No need to do three if statements:
echo "<table border='1'>";
for ($i = 1; $i <= 100; $i++) {
echo "<tr><td>{$i}</td><td>";
if ($i % 3 == 0) echo "BY3 ";
if ($i % 5 == 0) echo "BY5";
echo "</td></tr>\n";
}
echo "</table>";
Update the code as given below
<?php $var = range(0, 100); ?>
<table>
<?php foreach ($var as &$number)
{
echo " <tr>
<td>$number</td>
<td>";
if($number % 3 == 0 && $number % 5 == 0)
{
echo "BY3 AND 5";
}
elseif ($number % 5 == 0)
{
echo "BY5";
}
elseif ($number % 3 == 0)
{
echo "BY3";
}
echo "</td></tr>";
}
?>
<?php
if($number % 5 == 0 && $number % 3 == 0) {
echo "BY3 AND 5";
} elseif ($number % 5 == 0) {
echo "BY5";
} elseif ($number % 3 == 0) {
echo "BY3";
} else{
echo "NOT BY3 OR 5";
}
?>
if($number % 15 == 0)
{
echo "Divisible by 3 and 5";
}
elseif ($number % 5 == 0)
{
echo "Divisible by 5";
}
elseif ($number % 3 == 0)
{
echo "Divisible by 3";
}
This is neater and completed to be run:
<?php
for ($i = 1; $i <= 100; $i++) {
if ($i % 15 == 0)
{
echo"Divisible by 3 and 5</br>";
}
elseif ($i%3==0)
{
echo"Divisible by 3</br>";
}
elseif ($i%5==0)
{
echo"Divisible by 5</br>";
}
else
{
echo $i,"</br>";
}
}
?>
<?php
for ($i = 1; $i <= 100; $i++) {
if ($i % 15 == 0) echo "This Number is Divisible by 3 and 5<br>";
else if ($i % 3 == 0) echo "This Number is Divisible by 3 only<br>";
else if ($i % 5 == 0) echo "This number is Divisible by 5 only<br>";
else{
echo "$i<br>";
}
}
?>
I'm not sure why, but I've created a function and somehow it never return anything.
It supposed to return a random number generated by using the mt_rand() function.
I modified the code and tried to run it on ideone and the results says runtime error, signal 11 (SIGSEGV)
Can someone tell me what's wrong with this?
<?php
function breedingTree($name, $N, $max)
{
include('config.php');
if ($N < $max AND $name > 0)
{
$sql = 'SELECT sire, dam
FROM '.$prefix.'owned_adoptables
WHERE aid = "'.$name.'"';
$res = mysql_query($sql);
list($s, $d) = mysql_fetch_row($res);
if (mt_rand(0,1) === 1) breedingTree($s, $N+1, $max);
else breedingTree($d, $N+1, $max);
}
elseif ($name <= 0)
{
if ($N === 0) return mt_rand(1,100);
elseif ($N === 1) return mt_rand(5,95);
elseif ($N === 2) return mt_rand(15,85);
elseif ($N === 3) return mt_rand(25,75);
}
}
echo breedingTree(355, 0, 4); // Return nothing
echo breedingTree(0, 0, 4); // Return random number between 1 - 100
?>
What if $N is not 0, 1, 2, or 3, or if ($N < $max AND $name > 0), or if ($N >= max AND $name > 0)? There is no return statement for those code paths.