PHP
<?php
$a = $offer->service_original_price_display;
$b = $offer->service_discounted_price_display;
$c = $a - $b;
?>
However I am getting this error: A non-numeric value encountered since service_original_price_display is '$500' and service_discounted_price_display is '$300'. I assume it is because both contains the dollar sign hence PHP is not able to perform the equation. Is there any solution to this? Thanks!
$a = str_replace("$", "", $offer->service_original_price_display);
$b = str_replace("$", "",$offer->service_discounted_price_display);
$ is string
use trim($a, '$'); to remove both side $ sign
You can use substr, if you know the first place is always occupied by a $ from an error standpoint, removing the $ ( with str_replace ) is probably better. But, as those answers where already posted. I get the scraps ( lol ).
$a = '$500';
$b = '$300';
$c = substr($a,1) - substr($b,1);
echo $c;
Related
I need to add numbers in php without changing the number format like below
$a = "001";
$b = "5";
$c = $a+$b;
Now the result comes like "6" but I need "006" if $a is "01" then the result should be "06".
Thanks
Technically speaking, the $a and $b in your example are strings - when you use the addition operator on them they converted to integers which can't retain leading zeroes. More details on string-to-number conversion are in the manual
Something like this would do it (assuming positive integer strings with leading zeros)
#figure out how long the result should be
$len=max(strlen($a), strlen($b));
#pad the sum to match that length
$c=str_pad($a+$b, $len, '0', STR_PAD_LEFT);
If you always know how long the string has to be, you could use sprintf, e.g.
$c=sprintf('%03d', $a+$b);
Here, % introduces a placeholder, 03 tells it we want zero padded to fill at least 3 digits, and d tells it we're formatting an integer.
Hope this would help you:
<?php
$a="001";
$b="5";
$l=max(strlen($a),strlen($b));
$c=str_pad($a+$b, $l,"0", STR_PAD_LEFT);
echo $c;
?>
For common case. Your code should looks like this.
$a = someFormat($original_a);
$b = someFormat2($original_b); // $b has different format.
$c = someFormat($a + $b);
Or, you need write formatRecognition function.
$a = getValueA();
$b = getValueB();
$c = someFormat(formatRecognition($a), $a + $b);
I am learning PHP and I just read about Assignment Operators and I saw this
$a .= 5 which means $a equals $a concatenated with 5. To test this I coded a simple script
<?php
$a = 12345;
$a .=6;
$b = 12345;
$b .=006;
$c = 12345;
$c .=678;
echo " a=$a and b=$b c=$c" ;
?>
the output was a=123456 and b=123456 c=12345678.My question is why the b isn't equal with 12345006? Is it because treats 6 == 006?
Because 006 is treated as the octal number 6, which is the converted to the string "6", and concatenated to "12345" (which is the number 12345 converted to a string).
Use $b .= "006", and the result will be 12345006.
Because numbers with leading zeroes are treated as octals. if the concatenation would've included the leading zeroes, then 006 would've been interpretted as a string, which makes no sense, because it hasn't got quotes around it.
If you want 006 to be treated as a string, write it as one: '006'. Leave it as is, and it'll be interpreted as an octal:
$b = $a = 123;
$a .= 8;
$b .= 010;//octal
echo $a, ' === ', $b, '!';//echoes 1238 === 1238!
Just as an asside: yes, those are comma's/. echo is a language construct to which you can push multiple values, separated by comma's. The upshot of using comma's is that the values aren't concatenated into a single string before being pushed to the output stream.
This means that it is (marginally faster). The downsides: there aren't any AFAIK.
In case you're interested in the internals of PHP, I've explain this a bit more detailed here...
Just try b as a string:
$b.= '006';
Then you will get output 12345006.
I have 2 variables that are defined as follow,
$a = abc.php? b=1a1a;
$b = abc.php? b=0;
How can I replace '1a1a' of variable $a into 0 of variable $b.
That means variable b will be $b = abc.php ? b=1a1a;
And, could anyone please tell me if $a = abc.php? b=1a1a; then how can I get $c = 1a1a, that means anything after 'b='.
try this
$a='abc.php?b=1a1a';
$b='abc.php?b=0';
$newstr=substr($a,strpos($a,'=')+1);
$replacestr=substr($b,strpos($b,'=')+1);
$pattern='/('.$replacestr.')/';
echo preg_replace($pattern, $newstr, $b);
Note:- THis will replace everything after = in your $b with everything after = in your $a
Update 2
If I understood your second question properly then i think you want this
$a='abc.php?a=2a2&b=1a1a' ;
$b='abc.php?a=2a2&b=0';
$newstr=substr($a,strripos($a,'=')+1);
$replacestr=substr($b,strripos($b,'=')+1);
$pattern='/('.$replacestr.')/';
echo preg_replace($pattern, $newstr, $b);
Here it will replace everything after the LAST = it finds in string $b with everything after the LAST = it finds in string $a
I'm new to php and mysql. Im following a tutorial from phpacademy on youtube. There is a section that is of form data, but everyone who followed the tutorial (myself included) got undefined index errors. I fixed it with & (ampersand) symbol. But what does it do in the follow circumstances?? Why does putting the & symbol in front of the $ stop the error? Is it the same as # and is it just suppressing the error or did it actually fix it?
$submit = &$_POST['submit'];
It means instead of assigning the value to $submit, assign a reference to the value to $submit.
If you are familiar with C and pointers, it is like a pointer to the value that is automatically dereferenced when you access it. However, PHP doesn't allow things pointer arithmetic or getting the actual memory address (unlike C).
$a = 7;
$b = $a;
$c = &$a;
$c = 5;
echo $a, $b, $c; // 575
CodePad.
To stop the error you mentioned, you can use a pattern such as...
$submit = isset($_POST['submit']) ? $_POST['submit'] : 'default_value';
...or...
$submit = filter_input(INPUT_POST, 'submit');
How can I cast to integer in PHP using only 1 or two chars?
If I have the operation $a = (int) $b; it will result using those two (or one) chars:
$a = <*insert the needed 1 or 2 chars*> $b;
I need only to cast to integer. Thank you.
Prefix a + (the unary plus):
$a=+$b
A pretty common trick for code-golfing that also works in PowerShell and other languages.
$ php -r "var_dump('4');"
string(1) "4"
$ php -r "var_dump(+'4');"
int(4)
$a = sprintf("%02d", $b);
If you need to pad it with something other than zero, replace the zero with the character you need.
You can make a function, I guess. Of other type of shorter cast I'm unaware.
function z($i)
{
return (int)$i;
}
$a = z($b);