When I run my variable array with foreach,I have found that echo string is not display when using with addition operator.But echo only assign operator can display.What is the reason that can't echo string with using addition operator ?
<?php
$key = 3;
echo $key+1;echo "<br>"; // 4
echo "The answer is ".++$key; // The answer is 4
echo "The answer is ".$key+1; // 1
//last echo is why can't display string and not getting 4
?>
That's because you're doing a math operation with a string.
"The answer is ".$key+1 is the same as
"The answer is 3" + 1 which equals 1;
You need to use () to clearify the scope
$key = 3;
"The answer is ".($key+1) === "The answer is 4"
Also
echo "The answer is ".++$key; // The answer is 4
echo "The answer is ".($key+1); // This would be 5, beause you're incrementing $key by 1 beforehand
Related
This question already has answers here:
How can I separate a number and get the first two digits in PHP?
(7 answers)
Closed 3 years ago.
Following are my PHP variables with values,
$value = 234333;
$value = 344665;
$value = 456325;
How to check if number start with 2 or 3 or 4.
If value start with 2 print "this is 2"
If value start with 3 print "this is 3"
If value start with 4 print "this is 4"
You can treat the value as a string and then use substr to check the first character.
Something like this would suffice:
function checkStartsWith($value) {
$result = "";
switch (substr($value, 0, 1)) {
case "2":
case "3":
case "4":
$result = "this is " . substr($value, 0, 1);
break;
}
return $result;
}
How you could call it:
echo checkStartsWith(234333);
echo checkStartsWith(344665);
echo checkStartsWith(456325);
Outputs:
this is 2
this is 3
this is 4
<?php
$cars=array("Volvo","BMW","Toyota","abc","xyz");
?>
Hello everyone, i have following array,above is the code,How can i search and get total number of values after "BMW".please help me with the above problem.
To clarify, I want the answer 3 in this case and if the value does not exist I would like the answer 5
This would work
echo count($cars) - (array_search('BMW', $cars) + 1);
This would be a little safer, just in case the value does not exist in the array.
$cars=array("Volvo","BMW","Toyota","abc","xyz");
if ( array_search('BMW', $cars) !== FALSE ) {
echo count($cars) - (array_search('BMW', $cars)+ 1);
}else{
echo 'BMW Does not exist in the array';
}
If you want the answer 5 if the item does not exist this might be what you want
echo count($cars) - (array_search('BMWX', $cars) !== FALSE ? array_search('BMWX', $cars)+ 1 : 0);
Or to simplify
echo count($cars) - array_search('BMWX', $cars);
will also give the answer 5
This question already has answers here:
What is the most efficient way to count all the occurrences of a specific character in a PHP string?
(4 answers)
Closed 5 years ago.
I am trying to separate 2 symbols from a string and then
count how many of these symbols there are.
So if i had : 1110001110000
Then it should find that there are 6= 1's and 7= 0's
So This is what I have tried:
Essentially what I need, is to read the string indexes in code would be string[$i] then IF there is a 1 or a 0 count it.
I tried using a for-loop
for ($i=0; $i < $getInput[$i] ; $i++) {
if ($getInput[$i] == 1) {
echo "ONE";
} elseif ($getInput[$i] == 0) {
echo "ZERO";
}
}
Here im trying to echo out ONE for everytime ther is a 1 and ZERO for everytime theres a zero.
$counter = 0;
foreach ($getInput as $key) {
echo $key;
}
here i tried to utilize a foreach, here I am not really declaring to see for One index, i tried putting a for each in a for but needless to say, it didn't work.
Using substr_count, you can do this in a fairly straightforward way:
echo substr_count("1110001110000", '1'); //Echos 6
echo substr_count("1110001110000", '0'); //Echos 7
Substr_count.
http://php.net/manual/en/function.substr-count.php
$str = "1110001110000";
Echo "there is " .substr_count($str, "0") ." zeros \n";
Echo "there is " .substr_count($str, "1") ." ones \n";
https://3v4l.org/BQEiR
If you want to output it as your code implies (one one one zero) you can use numberformatter.
Here I split the string to an array and loop through it and output the spellout of each number.
$str = "1110001110000";
$arr = str_split($str);
$nf = new NumberFormatter("en", NumberFormatter::SPELLOUT);
Foreach($arr as $numb){
echo $nf->format($numb) . " ";
}
Output:
one one one zero zero zero one one one zero zero zero zero
https://3v4l.org/nHX59
This question already has an answer here:
PHP how to get a random number that is different from values of an array
(1 answer)
Closed 7 years ago.
I have an array of numbers and I need to make a random number then check if that number is not in the array, If it is make another random number and then check that number to see if its in the array.
example.
array = 1, 2, 4, 5, 7
rand(1, 7) = 3 or 6
if rand(1, 7) = 1, 2, 4, 5 or 7 it would run again until it returned 3 or 6.
anyone know how to do this in php?
You may simply generate a random number and check if it is already in the array
$in = [1, 4, 7, 9];
do {
$rand = rand($min, $max);
} while(in_array($rand, $in));
echo $rand, ' is random, but not in the input array';
The above code generates a random integer that is insides the bounds defined in $min and $max. If the value already exists inside the array a new random value is fetched and compared to the input array.
Note: While the above is the minimal working code you may create an endless loop if your input array contains all possible values(Thanks #Action Dan). You didn't state in your question whether this is possible or not. If it is possible you need to work around this. Either by limiting the the maximum tries or validating the input array before and issuing an error message or increasing the 2nd parameter of rand.
Example(validating, only recommended for smaller arrays):
$in = [1,2,3,4,5];
$min = 1;
$max = 5;
if(range($min, $max) === $in) {
echo 'No possible value in range';
exit;
}
// code from above
<?php
$numbers = array(1,2,3,4,5,6,7);
$rand = rand(1,10);
if (in_array($rand, $numbers))
{
echo "Match found";
}
else
{
echo "Match not found";
}
echo "<br />" . $rand;
?>
In this sample I had $rand intentionally have more than 7, just to make sure the code is working well and "Match not found" is printed..
in_array() function checks if the value of $rand exists in $numbers if true.. prints "Match found" if not prints "Match not found".
Hope this helps..
$val=3.1;
echo $val;
How can i get the result 4 from $val.
Use ceil
<?php
echo ceil(4.3); // 5
echo ceil(9.999); // 10
echo ceil(-3.14); // -3
?>
Reference: http://in2.php.net/manual/en/function.ceil.php
Ceil function is what you want. Just do the following
ceil($val);
P.S. if you want it to be always an integer number that is bigger by 1 than the previous (even if the number is Integer), you can do something like this:
$a = ( ceil($val) == $val) ? ($val + 1) : ceil($val);