How to add a value to an existing value in php - php

Consider my variable $result to be 01212.... Now if i add 1 to my variable i get the answer 1213,but i want it to be 01213.... My php code is
echo sprintf('%02u', ($result+1));
Edit:
Answers to this question works.. But what happens if my variable is $result to be 0121212
in future...

You can use %05u instead on %02u
echo sprintf('%05u', ($result+1));
EDIT:
To generalize it:
<?php
$result = "0121211";
$len = strlen($result+1) + 1;
printf("%0${len}d", ($result+1)); // print 0121212
?>

you could try:
str_pad($result, 5, "0", STR_PAD_LEFT);

Maybe I'm missing something here but it could be as simple as
$result = '0'. ($result+1);
edit:
$test = array('01212', '0121212', '012121212121212', '-01212');
foreach( $test as $result ) {
$result = '0'.($result+1);
echo $result, "\n";
}
prints
01213
0121213
012121212121213
0-1211
( you see, there are limitations ;-) )

Read here: http://php.net/manual/en/function.sprintf.php
in sprintf, you also has to specify length you want - so in your case, if you want any number to be shown 5chars long, you haveto write
echo sprintf ('%05d', $d); // 5 places taking decimal
or even better
printf ('%05d', $d);

Seems like you want to have a '0' in front of the number, so here would be the simplest :P
echo '0'. $result;
Or if you insist on using sprintf:
$newresult = $result + 1;
echo sprintf('%0'.(strlen(strval($newresult))+1).'u', $newresult);

Related

Tidy up PHP echo code [duplicate]

This question already has answers here:
How can I combine two strings together in PHP?
(19 answers)
Closed 5 years ago.
Apologies in advance though I've tried and failed several different things and obviously I'm not a php pro just yet.
I'm looking for a way to tidy up my code here, I'm pretty certain I don't need to continually type "echo" for each line but can't work out how to combine my code to achieve the same result, any ideas?
<?php
$values = get_field('bothscoreno');
$url = "$values";
$arr = str_split("$url, PHP_URL_QUERY");
if ($url) {
echo $arr[11];
echo $arr[12];
echo $arr[13];
echo $arr[14];
echo $arr[15];
echo $arr[16];
} else {
echo 'No';
}
?>
Thanks in advance.
The code you posted probably has bugs, because the way it's written, it looks like it's intended to do something different from what it is actually going to do.
str_split() will take a string and output an array of single characters. It looks like you're trying to pass it two parameters, but enclosing them in quotes means it's actually just a single string.
Thus, if $url is equal to abc, your str_split() call will output an array of a, b, c, ,, , P, H, P, _, U, R, L, _ ... etc.
I don't think that's what you intended.
However, if it is what you intended, then you are splitting the string, only to re-join some of the characters back together again with echo. You can therefore can simplify the whole thing as follows:
$url = get_field('bothscoreno');
if ($url) {
echo substr($url, 11, 6);
} else {
echo "No.";
}
If I'm right and this isn't what you actually want to do, then I suggest either editing the question to clarify or asking a whole new one.
use for loop and start index from 14 to echo your result
$values = get_field('bothscoreno');
$url = $values;
$arr = str_split("$url, PHP_URL_QUERY");
$string = '';
if ($url) {
for($i = 11;$i <= 16;$i++){
$string .= $arr[$i];
}
} else {
$string = 'No';
}
echo $string;
Use the point as concatenation operator
echo $arr[11].$arr[12]
in PHP you're able to use a . as concatenation operator.
See the code below:
<?php
$values = get_field('bothscoreno');
$url = "$values";
$arr = str_split("$url, PHP_URL_QUERY");
if ($url) {
echo $arr[11].
$arr[12].
$arr[13].
$arr[14].
$arr[15].
$arr[16];
} else {
echo 'No';
}
Hope this helps!
Use array_slice
<?php
$values = get_field('bothscoreno');
$url = "$values";
$arr = str_split("$url, PHP_URL_QUERY");
if ($url) {
$subArr = array_slice($arr, 11, 6);
print_r($subArr);
// Or...
foreach ($subArr as $val) {
echo $val;
}
} else {
echo 'No';
}
?>

Reverse function of str_pad()

I have a simple code here using str_pad() function that will add 0 prefixes to a number. This is my code:
$num = 456;
$test = str_pad($num, 6, '0', STR_PAD_LEFT);
echo $test;
output:
000456
What if I want to reverse the code? If my number is set to $num = 000456, then it will output 456 only? Hoping for your kind consideration for my noob question. Thanks
Simply
$val = ltrim($your_num, "000");
$val = (int)$your_num;
Unless i am missing something, this should do the trick.
This helped me.
$str = "0011";
echo (int)$str;
and output is 11

PHP-separate the variables value from the position of character '+'

I have variable of type varchar having values $pointmoney=356+2311;
pointmoney is a field in database from where i am retreiving this value .
now i have separte two variable $point and $money
where i want to store $point=356 and $money=2311 from $pointmoney=356+2311.(i.e separate values from '+').
If any 1 knows any similar function then pls answer.
hope i ellaborated well to understand my query . if anything is unclear pls feel to comment .
Use explode():
$pointmoney = "356+2311";
list($point, $money) = explode('+', $pointmoney);
you explode function like
$pointmoney = "356+2311";
$arr_money = explode("+",$pointmoney);
echo $point = $arr_money[0];
echo $money= $arr_money[1].
hope this will sure help you,
You can use explode() reference PHP explode
$pointAndmoney = "356+2311";
list($point,$money) = explode('+',$pointAndmoney); // now you have different variable for each
$arr = explode("+","54+99");
$point = $arr[0];
$money = $arr[1];

PHP concatenate variable

Might be an easy question for you guys. can't find it on google.
I am trying to concatenate two variables name;
$i=0;
for ($i=0;$i<5;$i++){
if($array[$i]>0){
$test.$i=//do something
}else{
$test.$i=//do something
}
}
//echo $test0 gives me nothing.
//echo $test1 gives me nothing.
I know I can't use $test.$i but don't know how to do this.Any helps? Thanks!
try ${$test.$i} = value
EDIT: http://php.net/manual/en/language.variables.variable.php
I'm assuming that the variables are called $test0, $test1, ..., $test5. You can use the following:
${"test".$i}
Though, might I suggest that you make $test an array and use $i as the index instead? It's very odd to use $i as an index to loop through a list of variable names.
As an example, instead of:
$test0 = "hello";
$test1 = "world";
Use:
$test[0] = "hello";
$test[1] = "world";
Try this:
for ($i=0;$i<5;$i++){
$the_test = $test.$i;
if($array[$i]>0){
$$the_test=//do something
}
else{
$$the_test=//do something
}
}
This might work:
$varName = $test . $i;
$$varName = ...
Can i ask where for this is neccesary?

Remove last character from a variable and then multiply against another variable

I have a whole bunch of percentages stored as XX% (e.g. 12%, 50%, etc..) I need to remove the percentage sign and then multiply the percent against another variable thats just a number (e.g. 1000, 12000) and then output the result. Is there a simple way to strip the percentage sign and then calculate the output with PHP? Or should I consider some sort of JS solution?
You could use rtrim():
$value = ((int) rtrim('12%', '%')) * 1000';
Edit
You don't strictly need to call rtrim() , as it casts to an int ok with the percentage sign. It is probably cleaner to strip it though.
var_dump (12 === (int) '12%');
//output: bool(true)
You can make use of preg_replace_callback as:
$input = '12%, 50%';
$input = preg_replace_callback("|(\d+)%|","replace_precent",$input);
echo $input; // 12000, 50000
function replace_precent($matches) {
return $matches[1] * 1000;
}
Try this:
$number = str_replace('%', '', '100%');
$result = intval($number) * 5000; // or whatever number
echo $result;
If you use trim() or str_replace() in PHP you can remove the percent sign. Then, you should be able to multiply the resulting number (php is weakly typed after all).
<?php
$number = str_replace("%", "", $percentString);
$newNumber = ((int) $number) * 1000;
echo $newNumber;
?>
You can use str_replace. You can also pass an array of subjects into str_replace to have them all replaced.
<?php
$number = str_replace("%", "", $percentage);
$result = $number * $other_var;
print $result;
?>
<?php
$input=array('15%','50%','10.99%','21.5%');
$multiplier=1000;
foreach($input as $n){
$z=floatval($n)*$multiplier;
print("$z<br>");
}
?>

Categories