i'm realy done with this. My problem is the following:
code:
<?php
$highest = 0;
$string = "a.1";
$array = explode(".",$string);
$highest = $array[1];
$final = "secound value is: ".$highest++;
echo $final;
?>
All i want is adding something to the number in the $string. So as a result it should echo 2. However it echos 1.
Whats wrong?
Use pre-increment:
$final = "secound value is: ".++$highest;
More info: Incrementing/Decrementing Operators
Try the below code
<?php
$highest = 0;
$string = "a.1";
$array = explode(".",$string);
$highest = $array[1];
$final = "secound value is: ". ++$highest;
echo $final;
?>
The reason is, $highest++ is the post-increment. It will increment the value only after its usage. And ++$highest is pre-increment will increment the value first and then use it
You are doing a post increment. If you do a pre-increment, you will get 2 as your result. Is this what you want?
$final = "secound value is: ".++$highest;
Just to put an answer out of the box. You can save yourself exploding and an array that you may not need/want.
$highest = substr($str, 2) + 1;
There is a difference between pre-increment and post increment.
pre-increment-(++$highest)
At first increase the value by one then executes the statement.
post-increment-($highest++)
At first executes the statement then increase the value by one.
So in this case You have to use pre-increment.Then line will be executed after increment of the value .
Try this code:
<?php
$highest = 0;
$string = "a.1";
$array = explode(".",$string);
$highest = $array[1];
$final = "secound value is: ".++$highest;
echo $final;
?>
Related
I am struggling to get the lowest value based on the answer of the client.
$client_answer = 28;
$array = array(10,20,30,40,50);
The answer that should be given is: 20
So every answer should be rounded down to the lower number.
Other examples:
$client_answer = 37;
$array = array(10,20,30,40,50);
answer should be 30.
$client_answer = 14;
$array = array(10,20,30,40,50);
answer should be 10.
$client_answer = 45;
$array = array(10,20,30,40,50);
answer should be 40.
Is there a php function that I can use for this?
If not how can this be accomplished?
You can filter the array to only contain values equal to or below the given value $client_answer, then use max() on the filtered array.
$value = max(array_filter($array, function($v) use ($client_answer) {
return $v <= $client_answer;
}));
Live demo at https://3v4l.org/KHB8r
This might be a very stupid answer, but in this specific case, you are trying to truncate the unit number? Why not try this:
$client_answer = intdiv( $client_answer, 10) * 10;
Divide by 10, get rid of the last digit, and multiply again.
EDIT : typo
Only this rounds correctly
$client_answer = 28;
$array = array(10,20,30,40,50);
rsort($array,1);
$min_diff = max($array);
$closest_val = max($array);
foreach($array as $val)
{
if(abs($client_answer - $val) < $min_diff)
{
$min_diff = abs($client_answer - $val);
$closest_val = $val;
}
}
echo $closest_val;
my question is how to display string as a pattern using php loops like, if the string is computer,on first iteration it will display "c" and then second iteration it will display like "co" and so on.My following code given below.please give a solution.
<?php
$array = "computer";
$count = strlen($array);
for($i=0;$i<=$count;$i++)
{
echo $array[$i]."<br>";
}
?>
output will print like this
c
co
com
comp
compu
$array = "computer";
$count = strlen($array);
for($i=0;$i<=$count;$i++)
{
echo substr($array,0,$i+1)."<br>";
}
$array = "computer";
$count = strlen($array)-1;
$out='';
for($i=0;$i<=$count;$i++)
{
$out .= $array[$i];
echo $out."<br>";
}
Have a nice day :-)
You can use substr($array, 0, $i + 1) instead of $array[$i]. Visit to: PHP: substr for more information.
this is my problem 1 here: Handle number in string PHP . I solved it.
Now, i see new problem, you can see the picture:
I want to get only number, not date (500000 and 200000) and sum it.
This is my code without date:
$total= 0;
$ex = explode(' ',$_POST['txtSalary']);
function total($ex) {
global $total;
return $total+=$ex;
}
array_map('total',$ex);
echo $total."<br/>";
I try so much but no result, hope you can help me. Thank you!
I assume that your $_POST['txtSalary'] look like below:-
$_POST['txtSalary'] = '-27/07/2016: 5000000
-01/08/2016: 2000000';
So do like below:-
<?php
$_POST['txtSalary'] = '-27/07/2016: 5000000
-01/08/2016: 2000000';
$array = explode(PHP_EOL, $_POST['txtSalary']);
print_r($array);
$sum = 0;
foreach($array as $arr){
$sum += explode(': ',$arr)[1];
}
echo $sum;
Output:- https://eval.in/612692
Using the following example in PHP:
$priv['PAGE_A'] = 11;
$priv['PAGE_B'] = 22;
$priv['PAGE_C'] = 33;
$priv['PAGE_D'] = 44;
1) I would like to iterate on the 4 values in $priv. Would 'foreach' be the correct way to do it?
2) If the value is higher than a given number, I would like to echo the index of this value. Not sure how to do it. The comparaison must be INT (not string).
Ex. using "30" it would output:
PAGE_C
PAGE_D
Is it possible? Or maybe I am not using the correct container for what I'm trying to do ?
PS. How would you call the type of "$priv" in this example ? An array ? An indexed variable ? A dictionary ? A list ?
Thank you.
basically:
<?php
function foo($var){
$priv['PAGE_A'] = 11;
$priv['PAGE_B'] = 22;
$priv['PAGE_C'] = 33;
$priv['PAGE_D'] = 44;
$out='';
foreach ($priv as $k=>$v){
if ($v >$var){
$out .= $k.'<br>';
}
}
return $out;
}
echo foo('30');
demo: http://codepad.viper-7.com/GNX7Gf
Just create an array with the letters to iterate over.
$letters = array('A','B','C','D');
for($i=0;$i<count($letters);$i++) {
if($priv['PAGE_' . $letters[$i]] > /*value*/) {
echo $priv['PAGE_' . $letters[$i]];
}
}
$priv is an array.
Also, it's not too clear to me what you are exactly trying to do. Are you trying to echo the value of the array element if it's greater than a constant value?
We could do it using PHP builtin array functions. Its good to use builtin functions if possible in case of performance.
array_walk will do the trick for you. In this case $priv is an associative PHP array. Following is the one line script that will do what you want to achieve:
$input = 30;
$priv['PAGE_A'] = 11;
$priv['PAGE_B'] = 22;
$priv['PAGE_C'] = 33;
$priv['PAGE_D'] = 44;
array_walk($priv, function($value, $key, $input){ if($value > $input) echo $key . '<br>';}, $input);
I have this two digit number i.e. 50/20 which is seperated by slash and stored in one column of database.
$value = '50/20';
I want to get separate number as
$num1 = 50;
$num2 = 20;
and sum as
$sum = $num1+$num2;
Is there any solution for to separate those combine numbers.
Use explode
Try like this
$value = '50/20';
$arr = explode('/',$value);
$sum = $arr[0]+$arr[1];
//Output
$arr[0] contains 50
$arr[1] contains 20
You can check this by simply doing print_r($arr);
You can do also something like:
$value = '50/20';
$sum = array_sum(explode('/', $value));
echo $sum; // 70
$exp = explode('/',$value);
$value1 = $exp[0];
$value2 = $exp[1];
$sum = $value1 + $value2;
$value = '50/20';
$arr = explode('/',$value);
$num1=$arr[0];
$num2=$arr[1];
$sum=$num1+$num2;
print_r($sum);
Output
70
Explode Function is used to convert a sting to array.
This should work for you.
$value = "50/20";
$numbers = preg_split("/\//",$value);
$sum = $numbers[0]+$numbers[1];