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
Related
This question already has answers here:
PHP: pass a variable to a function, do something to the variable, return it back
(5 answers)
Closed 3 years ago.
How come the output of these two functions are not the same? Please enlighten me on this one.
first code:
function multiply_itself(&$number) {
$number *= $number;
return $number;
}
$my_num = 10;
echo "$my_num" . "<br>"; //Output 10
multiply_itself($my_num);
echo "$my_num"; //Outputs 100
second code:
function doubled($integer) {
$integer *= $integer;
return $integer;
}
$integer = 20;
doubled($integer);
echo "$integer"; //Outputs 20, why not 400?
The second example outputs 20 because the parameter is not passed by reference like in the first example.
Change the function signature to function doubled(&$integer) or use its return value $integer = doubled($integer); and it prints 400.
This question already has answers here:
Formatting a number with leading zeros in PHP [duplicate]
(11 answers)
Closed 4 years ago.
Modulus (%) operator return single zero. It is correct but i want to 2 zero after using modulus (%) operator. I don't want to use if condition.
$k = 60;
$result = $k%60;
echo "Result = ".$result;
Output
Result = 0
Expected output
Result = 00
try it :
$k = 60;
$result = $k%60;
echo str_pad($result, 2, '0', STR_PAD_LEFT);
or test this method :
$result=0
$pad_length = 2;
$pad_char = 0;
$str_type = 'd'; // treats input as integer, and outputs as a (signed)
decimal number
$format = "%{$pad_char}{$pad_length}{$str_type}";
// output and echo
printf($format, $result);
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
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
This question already has answers here:
ask about php summarize 01 + 01 = 02
(2 answers)
Closed 7 years ago.
How get the number with format is leading zero ex:01
and how to make format number like that my problem is from this query
$try= $this->login->get_id_child('00',3);
$count_try = count($try);
$id ="00";
$new = $id.$count_try;
echo $new;
the result from echo $new is 003
data in array $try=array(000,001,002) and try is count by $count_try the result is 3. but I want to make the result from $count_try is 03 so I can join that variable to my id so I can get $new=00003 but if the data in
$try=array(000,001,002,003,004,005,006,007,008,009);
so the value of $new is 0010
You can use str_pad -
echo str_pad($new, 2, "0", STR_PAD_LEFT);
str_pad()
Update
$count_try = 10;
$new = str_pad($count_try, 4, "0", STR_PAD_LEFT);
echo $new;
Try this :
$id ="0010";
$new = (int) $id + 1;
$new = str_pad($new, 4, "0", STR_PAD_LEFT);
echo $new; //0011