what is the different between this in php? - php4

what is the difference between
$totalprice += $product['price'] * $product['count'];
and
$totalprice = $product['price'] * $product['count'];
both give the same result. so what's the use of (+=) ?

+= is a shorthand for adding the result to the target. The first one is equivalent to:
$totalprice = $totalprice + ($product['price'] * $product['count']);
There are also other compound operators -=, *=, /=, etc.

They only give the same result if $totalprice starts off at 0 or uninitialised
The += syntax is shorthand for the following:
$myvar += a;
is equivalent to
$myvar = $myvar + a;

The += takes $totalprice and adds $product['price'] * $product['count'] to it.
The = asigns the value of $product['price'] * $product['count'] to $totalprice.
If you are getting the same result, its because $totalprice started off equal to 0.

If $totalprice is zero to start, then they're the same. Otherwise, they're different.
As others have pointed out, $i += $j is shorthand for $i = $i + $j.

Related

Perl <<= and ^= Meaning

Hello I´m trying to convert a bunch of Perl scripts to C#.
A have this piece of code and I don´t know what <<=, ^=, &= means. Can someone please tell me the meaning of the part (???) too?. Thanks in advance.
$cwl = 6;
$sr = 1;
for ($i=0; $i<$gf::field; $i++) {
$gf::log[$sr] = $i;
$gf::alog[$i] = $sr;
$sr <<= 1;
if ($sr & (1<<$cwl)) {$sr ^= $prim} (???)
$sr &= $gf::field;
}
These are:
<<= = Left shift equal operator, shifts the source variable's bits by n places to the left and assign back to source variable.
^= = Bitwise XOR equal operator. XORs the source variable by the right hand variable, and assigns to the source variable.
&= = Bitwise AND equal operator. ANDs the source variable and the right hand variable, and assigns the result to the source variable.
The C# equivalents from your code above:
Left Shift
# Perl code
$sr <<= 1;
// C# code
sr = sr << 1;
Bitwise XOR
# Perl code
$sr ^= $prim
// C# code
sr = sr ^ prim
Bitwise AND
# Perl code
$sr &= $gf::field
// C# code
sr = sr & gf.field // or whatever you've named $gf::field
Edit following OP comment
OP asked whether:
if ($sr & (1<<$cwl))
Can be translated to:
cwl=1 << cwl;
if (Convert.ToBoolean(sr) & Convert.ToBoolean(cwl))
I think you've made an error here. cwl is not reassigned in the original if statement in Perl.
In fact, the logic of the operation is to say:
Is sr bitwise AND 1 left-shifted by cwl true?
I'd be tempted to rewrite the Perl code to:
if ((sr & (1 << cwl)) != 0)
Why? The original statement ascertains whether the result of the if statement is true. Any non-zero value in Perl is true, therefore we need to perform the operation as per the Perl and test for a non-zero value.

References to arrays/objects in PowerShell

I use the reference Variable to pass parameters to functions and to manipulate values across functions using the same base variable.
For my other script this worked fine, and maybe this is just a thought problem here, but why this isn't working?:
$Script:NestedLists = #("test", #("test_level_2"))
function AddToReference
{
param([ref]$RefVar)
$RefVar.Value += #("hi")
}
AddToReference -RefVar ([ref]($Script:NestedLists[1]))
$Script:NestedLists[1]
I thought the output of $Script:NestedLists[1] would be "test_level_2" and "hi" but it is just "test_level_2"
This little change made it work
$Script:NestedLists = #("test",#("test_level_2"))
function AddToReference
{
param ([ref]$RefVar) ($RefVar.value)[1] += , "hi"
}
addtoreference ([ref]$Script:NestedLists)
$Script:NestedLists[1]
Why moving the [1] to $refvar made it work, I have no idea, I wish I had a better understanding. Also, this get's really tricky because if you add a value to the first array in the $script it moves the [1] to [2] etc...
I would personally do something to keep each array separate, and at the end just combine them as needed...
$a = "first","array"
$b = "second","array"
$script:nested = $null
$script:nested += , $a
$script:nested += , $b
The "+= ," combines each array in a nested array. so [0] would equal $a and [1] would equal $b

Best practice for creating default values in Powershell

I am new to Powershell, but i am curious what is the best practice for creating default variable in Powershell. This is an example, which i am referencing. In case if you just want to initialize default variables, without intention to pass any parameters to function. Which is better way number 1 or 2 or none of them. :)
1.
function test
{
param ([int]$x = 5,[int]$y = 14)
$x * $y
}
2.
function test
{
[int]$x = 5
[int]$y = 14
$x * $y
}
It just depends on your use-case. If you truly never intend to change the variables, #2 is correct.
I think you just need to ask yourself what future use-cases might be. Would changing the values break your function? The ability to supply parameters is very useful, if not now, perhaps in the future.
Basically, if you're using the variables as FINAL, #2 is fine, but in all other cases I would say #1 is more correct.
If your intention is to take parameters to your function, use Param(). By default, undefined values will be $Null
Function Test
{
Param(
[Int]
$X = 5,
[Int]
$Y = 14
)
Return $X * $Y
}
Function Test2
{
$X=5; $Y=14
$X * $Y
}
> Test2
>> 70
> Test
>> 70
> Test 5 20
>> 100

How does incrementation operator work within a loop

I've just started off with perl, and while trying out a few compound statements, I wrote this:
my $ct;
while ($ct++ < 10) {
print $ct;
}
It prints out:
12345678910
I was not expecting it to print 10. How does the logic for the loop really work?
According to perdoc, a TERM operator has the highest precedence. $ct gets incremented to 10 after iterating the loop where it is 9. When it becomes 10, while loop is supposed to exit. So why is 10 still printed out?
Think of it like
while ($ct < 10) {
$ct += 1;
print $ct;
}
(increment after comparison)
On the other hand, ++ on the left side of the variable will increment first, and then do comparison,
while (++$ct < 10) {
print $ct;
}
This is quite intuitive for someone with C background; from perldoc:
"++" and "--" work as in C. That is, if placed before a variable, they increment or decrement the variable by one before returning the value, and if placed after, increment or decrement after returning the value.
Its because you are using the postfix operator. its first compared then incremented.

Please explain me what is hapenning in line 3 of this code

Please explain me what is hapenning in line 3 of this code.
for my $i (0 .. $dim) {
for my $j (0 .. $dim) {
$adj->[$i][$j] = $adj->[$i][$j] ? [$j] : [];
The code loops over two dimensions in an array reference $adj. Presumably, $dim is the dimension, and $i and $j iterate over a list of numbers from 0 to $dim, e.g. 0,1,2,3,4,5.
For each combination of numbers, the value of that array element is checked for trueness, and is assigned a new value. If the value is false, it is assigned an array ref containing the index $j, otherwise an empty array ref [].
The conditional operator is used here, with the basic syntax
CONDITION ? FOO : BAR
if CONDITION then FOO else BAR
Presumably, the array ref $adj is supposed to contain array references, which is why it can simply check for trueness as a shortcut for defined $adj->[$i][$j].
This is the ternary operator, aka conditional operator.
If $adj->[$i][$j] is 0 (or undefined) then [] is assigned to $adj->[$i][$j], in the other cases $adj->[$i][$j] is assigened to $adj->[$i][$j].
perlop has this quote:
Ternary "?:" is the conditional operator, just as in C. It works much like an if-then-else.
If the argument before the ? is true, the argument before the : is returned, otherwise the argument after the : is returned.
for my $i (0 .. $dim) {
for my $j (0 .. $dim) {
Above for loops will itrate through the arry with dimension $dim x $dim
$adj->[$i][$j] = $adj->[$i][$j] ? [$j] : [];
if $adj->[$i][$j] is zero, then assign [] to $adj->[$i][$j] else assign $j (column value)