I am trying to compare two values but when I do it does not appear to work. I know what the values are so it should be reporting true. Even worse, if I take either one of the variables out and put the number in it works.
$data = simplexml_load_file('xml/heroes/hero.xml')
or die("Error: Cannot create object");
$hme = $data->hes->he->maxen;
$hce = $data->hes->he->curen;
$hac = $data->hes->he->lastac;
echo $hce . ' should not be greater than ' . $hme;
if($hce > $hme){
echo 'should be working';
}
Outputs:
773 should not be greater than 20
I think your variable are like this
$hce = "773";
$hme = "20";
Before comparing them do intval
if(intval($hme)>intval($hce))
Cast your strings to integers:
$hme = (int)$data->hes->he->maxen;
$hce = (int)$data->hes->he->curen;
$hac = (int)$data->hes->he->lastac;
I think you took them as strings.I think you need to convert them to integer.
Simple function to do that:
int atoi(char *s)
{
int val = 0;
while (*s)
{
val *= 10;
val += (*s) - '0';
s++;
}
return val;
}
Related
I am using MongoDB to store values as Decimal128 and using PHP with Twig to display them. My question is there a format the output with a thousands separator? I have tried using number_format, but that doesn't work because the value is a string not a int or float. I don't want to type cast the value because I want the value to be precision.
Examples:
1000.382 = 1,000.382
99.01 = 99.01
1900000 = 1,900,000
I wrote a function to do this. Still wondering if there is a better way to do it.
<?php
$n = '12345.001';
echo fNumString($n);
function fNumString ($number_string) {
$ex_num = explode('.', $number_string);
$whole_num_len = strlen($ex_num[0]);
$formated = '';
if ($whole_num_len > 3) {
for ($i = 0; $i < $whole_num_len; $i++) {
$formated .= $ex_num[0][$i];
if ((($whole_num_len - ($i + 1)) % 3) == 0 && $whole_num_len != ($i + 1))
$formated .= ',';
}
} else
$formated = $ex_num[0];
if (count($ex_num) == 2)
$formated .= '.' . $ex_num[1];
return $formated;
}
You could:
split the number string into integer and decimal parts using explode,
reverse the integer part using strrev,
add a thousands separator every 3 digits within that integer part using preg_replace,
reverse the integer part back using strrev,
return it concatenated with the decimal separator and the decimal part
Code:
function fNumString(string $numberString, string $decimalPoint = '.', string $thousandsSeparator = ','): string
{
[$integerPart, $decimalPart] = array_pad(explode('.', $numberString, 2), 2, null);
$integerPart = strrev(preg_replace('/\d{3}(?=\d)/', '\0' . $thousandsSeparator, strrev($integerPart)));
return $integerPart . ($decimalPart ? $decimalPoint . $decimalPart : '');
}
Demo: https://3v4l.org/m3jUC
Note: you could leave out the last 2 parameters of number_format, I like keeping them out for clarity (and in case they change the default values later for some reason).
Actually I want to Display Flags Name of all fields which is belongs to a table. This is Our script, from which i am performing this action.
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("DatabaseName");
$result = mysql_query("SELECT * FROM Table_Name");
$fields = mysql_num_fields($result);
for ($i=0; $i < $fields; $i++) {
$flags = mysql_field_flags($result, $i); echo $flags . "<br>";
}
mysql_free_result($result);
mysql_close();
?>
This works well, but the problem is this script is mysql version but i want mysqli version of the script. Is there any way to perform the same action by mysqli version of the script.
and also the question is - this script is using offset value in for loop. Is there any to way to display flags value without offset value
OR Insetead of using offset value can we use field name ?
this will fetch the flags
$mysqli=new mysqli('localhost','root','','database');
$result=$mysqli->query('SELECT * FROM table');
while($meta = mysqli_fetch_field($result)){
$flags=$meta->flags;
echo $flags."<br>";
print_r(get_flag1($flags));
echo '<br/>';
print_r(get_flag2($flags));
echo '<br/>';
}
function get_flag1($flag_value){
$result['NOT_NULL_FLAG'] = 1&$flag_value?1:0;
$result['PRI_KEY_FLAG'] = 2&$flag_value?1:0;
$result['UNIQUE_KEY_FLAG'] = 4&$flag_value?1:0;
$result['BLOB_FLAG'] = 16&$flag_value?1:0;
$result['UNSIGNED_FLAG'] = 32&$flag_value?1:0;
$result['ZEROFILL_FLAG'] = 64&$flag_value?1:0;
$result['BINARY_FLAG'] = 128&$flag_value?1:0;
$result['ENUM_FLAG'] = 256&$flag_value?1:0;
$result['AUTO_INCREMENT_FLAG'] = 512&$flag_value?1:0;
$result['TIMESTAMP_FLAG'] = 1024&$flag_value?1:0;
$result['SET_FLAG'] = 2048&$flag_value?1:0;
$result['NUM_FLAG'] = 32768&$flag_value?1:0;
$result['PART_KEY_FLAG'] = 16384&$flag_value?1:0;
$result['GROUP_FLAG'] = 32768&$flag_value?1:0;
$result['UNIQUE_FLAG'] = 65536&$flag_value?1:0;
return $result;
}
function get_flag2($flag_value){
if(1&$flag_value)
$result[]='NOT_NULL_FLAG';
if(2&$flag_value)
$result[]='PRI_KEY_FLAG';
if(4&$flag_value)
$result[]='UNIQUE_KEY_FLAG';
if(16&$flag_value)
$result[]='BLOB_FLAG';
if(32&$flag_value)
$result[]='UNSIGNED_FLAG';
if(64&$flag_value)
$result[]='ZEROFILL_FLAG';
if(128&$flag_value)
$result[]='BINARY_FLAG';
if(256&$flag_value)
$result[]='ENUM_FLAG';
if(512&$flag_value)
$result[]='AUTO_INCREMENT_FLAG';
if(1024&$flag_value)
$result[]='TIMESTAMP_FLAG';
if(2048&$flag_value)
$result[]='SET_FLAG';
if(32768&$flag_value)
$result[]='NUM_FLAG';
if(16384&$flag_value)
$result[]='PART_KEY_FLAG';
if(32768&$flag_value)
$result[]='GROUP_FLAG';
if(65536&$flag_value)
$result[]='UNIQUE_FLAG';
return $result;
}
how it work
using & with these values to check the wanted flag it will output 0 if the flag is off or the value if the flag is on
for example to check UNIQUE_KEY_FLAG use $flags_value&4 it will output 0 if it off or 4 if it on
NOT_NULL_FLAG = 1
PRI_KEY_FLAG = 2
UNIQUE_KEY_FLAG = 4
BLOB_FLAG = 16
UNSIGNED_FLAG = 32
ZEROFILL_FLAG = 64
BINARY_FLAG = 128
ENUM_FLAG = 256
AUTO_INCREMENT_FLAG = 512
TIMESTAMP_FLAG = 1024
SET_FLAG = 2048
NUM_FLAG = 32768
PART_KEY_FLAG = 16384
GROUP_FLAG = 32768
UNIQUE_FLAG = 65536
explain
the flag is binary number like 10110011 every bit represent a flag state on or off 0 for off and 1 for on
for example to check unique key flag it's the third bit so when you use the and operator between 100B and 10110011B the result will be all zero except the wanted bit
so if this bit is zero all the number will be zero and this mean false to php
and if it's one the result will be 100B in php any number not zero will mean true to php
Try something inline with this
if($field_flags == false) {
for($j=0; $j<$num_fields; $j++) {
$field_flags[$j] = mysqli_fetch_field_direct($result, $j);
if(strpos($field_flags[$j]->flags, 'NOT_NULL') !== false) {
//Do something here
} else {
//Do something else
}
}
or see this
How can this be done with the same result using mysqli?
I'm getting error like PHP
Warning: Division by zero error.
I'm still and want to calculate some problems. If you can tell me, how can calculate antilog / log inverse with examples, I'll be more satisfied.
<?
if(isset($_POST['submit']))
{
$P=$_POST['P']; // P=100000
$rate=$_POST['rate']; // rate = 10.5
$R=($rate/100);
$N=$_POST['N']; // N = 78
echo $P ."<br>" ;
echo $R ."<br>" ;
echo $N ."<br>" ;
$NEW=($R/12); // NEW = .00875
$aa = (1+($NEW)); // aa = 1.00875
$ab = bcpow('$aa', '$N',16); // ab = 1.9729529246182467
$ac = ($ab-1); // ac = 0.9729529246182467
$ad = bcdiv('$ab', '$ac', 3); // Div by Zero Error
$M = ($P*$NEW) *($ad);
echo "The Installment is : " . $M . "<br>";
}
?>
The line with the problem:
$ad = bcdiv('$ab', '$ac', 3); // Div by Zero Error
The problem here is because $ab and $ac are in quotes. They shouldn't be.
Having them in quotes means that PHP sees $ab as being a string consisting of the characters $, a and b, instead of the numeric value of the $ab variable.
Remove the quotes, and it should start working.
The same applies to the bcpow() line.
bcpow('$aa', '$N',16); are you sure these variables get parsed? They are treated as a string in single quotes, and since there is no number they might be just bogus. (typing $aa^$n on a calculator will fail).
You can use bcpow("'".$aa."'", "'".$N."'",16); or try using double quotes.
I didn't understood your code as well, but here is the code that calculate Antilog / log of a value
<?php
$log = 11;
$e = 2.718281828;
$dn = 1;
// Calculate Antilog of $log = 11
$ans = pow($e, $log);
$antilog = round($ans*10000)/10000;
echo "<br /> Antilog : ".$antilog;
// Calculate Log of $log = 11
$ans = log($log)/$dn;
$log = round($ans*10000)/10000;
echo "<br /> Log : ".$log;
// Calculate Antilog / Log of $log = 11
echo "<br /> Antilog / Log : ".($antilog / $log);
?>
Result:
Antilog : 59874.1416
Log : 2.3979
Antilog / Log : 24969.407231327
Hope this help you, if else please provide more details (comments) on your code.
To narrow down where your issue is coming from:
string bcdiv ( string $left_operand , string $right_operand [, int $scale ] )
bcdiv will return Division by zero only if the right operand is 0, so you can backtrace this by assuming $ac is your culprint. Try echoing $ac before the bcdiv function call to ensure it is not empty.
I'm willing to bet that this line $ac = ($ab-1); // ac = 0.9729529246182467
$ac is going negative.
You should also remove your single quotes, don't take the manual so literal. You can still pass in an int or double and the function will convert it for you. even if you cast the variable as a double.
//WITHOUT QUOTES
$ab = '1.9729529246182467';
$ac = ($ab-1);
echo bcdiv($ab, $ac, 3);
//output (2.027)
//WITH QUOTES
$ab = '1.9729529246182467';
$ac = ($ab-1);
echo bcdiv($ab, '$ac', 3);
//output Warning (2): bcdiv() [function.bcdiv]: Division by zero
Hey so I'm making a factoring program and I'm wondering if anyone can give me any ideas on an efficient way to find what two numbers multiple to a specified number, and also add to a specified number.
for example I may have
(a)(b) = 6
a + b = 5
So essentially i just need a way to find the a and b values. In this case they would be 2 and 3.
Can anyone give me any ideas on where to start? Negative numbers must also be considered for use.
There is no need to loop, just use simple math to solve this equation system:
a*b = i;
a+b = j;
a = j/b;
a = i-b;
j/b = i-b; so:
b + j/b + i = 0
b^2 + i*b + j = 0
From here, its a quadratic equation, and it's trivial to find b (just implement the quadratic equation formula) and from there get the value for a.
There you go:
function finder($add,$product)
{
$inside_root = $add*$add - 4*$product;
if($inside_root >=0)
{
$b = ($add + sqrt($inside_root))/2;
$a = $add - $b;
echo "$a+$b = $add and $a*$b=$product\n";
}else
{
echo "No real solution\n";
}
}
Real live action:
http://codepad.org/JBxMgHBd
Here is how I would do that:
$sum = 5;
$product = 6;
$found = FALSE;
for ($a = 1; $a < $sum; $a++) {
$b = $sum - $a;
if ($a * $b == $product) {
$found = TRUE;
break;
}
}
if ($found) {
echo "The answer is a = $a, b = $b.";
} else {
echo "There is no answer where a and b are both integers.";
}
Basically, start at $a = 1 and $b = $sum - $a, step through it one at a time since we know then that $a + $b == $sum is always true, and multiply $a and $b to see if they equal $product. If they do, that's the answer.
See it working
Whether that is the most efficient method is very much debatable.
With the multiplication, I recommend using the modulo operator (%) to determine which numbers divide evenly into the target number like:
$factors = array();
for($i = 0; $i < $target; $i++){
if($target % $i == 0){
$temp = array()
$a = $i;
$b = $target / $i;
$temp["a"] = $a;
$temp["b"] = $b;
$temp["index"] = $i;
array_push($factors, $temp);
}
}
This would leave you with an array of factors of the target number.
That's basically a set of 2 simultaneous equations:
x*y = a
X+y = b
(using the mathematical convention of x and y for the variables to solve and a and b for arbitrary constants).
But the solution involves a quadratic equation (because of the x*y), so depending on the actual values of a and b, there may not be a solution, or there may be multiple solutions.
I'd like to increment a value in a 3-digits format.
For example:
$start_value = 000;
while () {
do something;
$start_value++;
}
In this way I have this result:
$start_value = 000;
$start_value = 1;
$start_value = 2;
$start_value = 3;
and so on
instead of '001', '002', '003'
How I can accomplish this result?
Using sprintf you can accomplish this with:
echo sprintf("%03d", $start_value++) . "<br>";
Hopefully that is what you were after.
Implementing it with your code:
$start_value = 000;
while () {
do something;
$start_value = sprintf("%03d", $start_value++);
}
You are making a big mistake here. 000 in code is an octal number. Any literal number starting with a 0 in many programming languages is considered an octal literal. If you want to store 000, you need a string, not a number.
$start_value = "000";
while((int) $start_value /*--apply condition --*/) {
do something;
$start_value = str_pad((int) $start_value+1, 3 ,"0",STR_PAD_LEFT);
}
It goes like this. In PHP there is no concept of datatypes everything is determined in runtime based on where and how it is used.
<?php
$start_value = 000;
while ($start_value < 10) {
//your logic goes here
$start_value++;
printf("[%03s]\n",$start_value);
}
?>
Output : [001] [002] [003] [004] [005] [006] [007] [008] [009] [010]
So you can do all the calculation. Where ever you want to print the value, you can use printf with format specifiers.!! Hope it helps.