Post by IP_CAM » Sun May 11, 2014 6:04 am

OUT_OF_STOCK_IMAGE.XML

In my out_of_stock XML Mod, I find different way's of writing an EQUAL ZERO Amount, wich one is better in PHP??
Perl used to rather tolerant in that point...

if($product['productQuantity']==0)
if($product['productQuantity']=='0')

more, the Mod only works if Stock Quantity equals ZERO, if someone preorders an item, the Stock will drop below ZERO and the MOD will not longer work

I tried:
if($product['productQuantity']<1)
if($product['productQuantity']=<1)
if($product['productQuantity']=<'1')
if($product['productQuantity']<'1')

but nothing seems to function correctly, it produced a <-error...
I appreciate your assistance!

Ernie

My Github OC Site: https://github.com/IP-CAM
5'200 + FREE OC Extensions, on the World's largest private Github OC Repository Archive Site.


User avatar
Legendary Member

Posts

Joined
Tue Mar 04, 2014 1:37 am
Location - Switzerland

Post by rph » Sun May 11, 2014 7:15 am

This is a much more complex subject than you'd think. Check out data types, type juggling, comparison operators, and type comparison tables in the PHP documentation for more depth.

The short answer is don't use any quotes. A single or double quote is a string data type. You're trying to compare integers or floats. PHP is a loosely typed language and will try to covert between different data types. This can lead to all kinds of unintended side effects so don't rely on this.

Also your less than comparison operator is wrong. Try code like this:

Code: Select all

// Loose "equal" comparison
// Integer 0, float 0.00, and string 0 will match but so will boolean false and null
if ($product['product_quantity'] == 0) {
    ...
}

// Strict "identical" comparison
// Use this if you KNOW the variable will be an integer - it can prevent above weirdness
if ($product['product_quantity'] === 0) {
    ...
}

// Less than or equal to
if ($product['product_quantity'] <= 1) {
    ...
}
 

-Ryan


rph
Expert Member

Posts

Joined
Fri Jan 08, 2010 5:05 am
Location - Lincoln, Nebraska

Post by IP_CAM » Sun May 11, 2014 9:46 am

Thank you very much, it's gonna take a while to get familiar with php-specific things...., after trying hard for so long getting to know Perl a little deeper...

A great disadvantage is, that I am not of english origin, I never attended such kinds of Schools either, it's all 'picked up' by listening, speaking, reading and writing only. Therefore, most english programmers 'terms' never really 'meant' much to me. Simple terms like hash, array, scalar, etc., or worse, an array was to me, as former Communication Product Dealer, more something like a directional antenna, Hash was something to smoke. Makes it even more complicated...

Still, It did not prevent me from intensively playing with those 'things', if I knew what they are here for and how they function, if I needed to. But such 'Limitations' make it kind of hard to read Programmer-Information created by english Experts, I need to see Samples in order to be able to understand their basic functions. Possibly even different versions, then, I get the point and can handle them. Usually !

So much to this, I just want to make sure you don't ask yourself, why this idiot asks such stupid Newbie-Questions, while trying to play 'Expert' to some of the Forum-Members, when it comes to other matters around OC.

best rgds, Ernie

My Github OC Site: https://github.com/IP-CAM
5'200 + FREE OC Extensions, on the World's largest private Github OC Repository Archive Site.


User avatar
Legendary Member

Posts

Joined
Tue Mar 04, 2014 1:37 am
Location - Switzerland

Post by rph » Sun May 11, 2014 12:09 pm

Many concepts in PHP will apply to programming in general. I originally started off in Perl before (primarily) moving to PHP so it can definitely be done.

To give you a quick rundown of the different data types (parenthesis is the shorthand PHP uses):

integer (int) - A whole number such as 7 or 2147. An example in OpenCart is the quantity of product being ordered.

float (double) - Any number with a decimal point in it such as 23.1 or 3.1415. A number like 1.00 is still a float even though its value is equal to an integer. In OpenCart this would be something like an order total.

boolean (bool) - True or false. Integers 1 and 0 can also be evaluated like booleans (true and false, respectively).

string (string) - Any quoted text, including things like integers and floats when they're enclosed by single or double quotes. In OpenCart a product name would be a string.

null (null) - No value. Literally nothing. Imagine going into a bank and asking how much money is in your savings. 0 could be a valid response. But what if you didn't even have an account at the bank? Then the answer would be null. Your savings isn't positive, negative, or zero. It doesn't exist at all.

array (array) - These are containers for values. Think of them like a big dresser which you're putting other things in. Arrays have two parts: keys and values. Keys are the location of a value. They are the "drawer" of the dresser. A value is what's being stored there, such as socks. So if I tell PHP I want the contents of the "second drawer" (the key) it will give me "shirts" (the value).

To make things more complicated you can put a dresser within a dresser drawer. Those are known as multidimensional arrays and you can keep putting one inside another like Russian nesting dolls.

object (object) - This is a more advanced concept but an (over)simplified way to think of objects is an array that can also do tasks (functions). A value that an object is storing is called a "property". A task it can carry out is known as a "method".

-Ryan


rph
Expert Member

Posts

Joined
Fri Jan 08, 2010 5:05 am
Location - Lincoln, Nebraska

Post by IP_CAM » Mon May 12, 2014 7:57 am

I'll keep this locally, thanks a lot!

Ernie

My Github OC Site: https://github.com/IP-CAM
5'200 + FREE OC Extensions, on the World's largest private Github OC Repository Archive Site.


User avatar
Legendary Member

Posts

Joined
Tue Mar 04, 2014 1:37 am
Location - Switzerland
Who is online

Users browsing this forum: Bing [Bot] and 340 guests