Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help converting some php code please

Status
Not open for further replies.

CADTenchy

Technical User
Dec 12, 2007
237
GB
Hi, I'm unfamiliar with PHP, and I'm trying to hand convert the PHP code below.

I'm having trouble understanding the $e and $n variables.
At first they seem to be strings, then they seem to become numbers.
I can only guess they are numbers the whole time?

Code:
    $pr=$le/2;  // $le is the length of $num below
    $n=substr($num,$pr);    //$num is a string containing numbers so I assumed $n was a string too?
    $e=substr($num,0,$pr);  //  ditto $e. are they converted to numbers here?
    $pr=pow(10.0,(5.0-$pr));
    $T1=ord(substr($lett,0,1))-65;  // here I clearly see letter to number conversion, so what about above?
    if($T1>8)
    {
        $T1=$T1-1;
    }
    $e=100000.0*($T1%5.0)+$e*$pr;  // now $e and $n appear to be numbers?
    $n=$n*$pr+100000.0*(4.0-floor($T1/5.0));

Steve (Delphi 2007 & XP)
 
PHP is what we call a loosely typed language. Which means variables don't actually have fixed types. Which means at any point you can treat a number as a string.

Code:
 $pr=$le/2;  // $le is the length of $num below
    $n=substr($num,$pr)[green]\\ This is getting a specific subset of characters from the $num variable.
So if $num where 123456 $n would end up being 456 

 [/green]
    $e=substr($num,0,$pr);  [green]Now it retrieves the first half of the $num variable, in my example above it would be 123[/green]
...

So all in all, Nowhere are they actually converted to or from anything. They are just used as different things.

Now because after all the processing the contents of the variables are still numeric, they can be used in mathematic operations.

But type conversion never occurs. That is the variables never change.

Its one of the most useful features PHP has. Being able to use variables based on their contents and not based on a type.


----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
While vacunita gave a good explanation, I would nuance it a bit.

It's not that there was no conversion, it's that the conversion/typecast is implicit. That is to say, in other languages, such as C or Pascal, you would have to include code to cast or convert the strings to integers before you could add them. In PHP, the interpreter does this for you automatically. So, for example, the two additions below are equivalent.
Code:
$a = "5";
$b = "4";
$c = $a + $b;            # Gives you int(9)
# Same thing, but with an explicit cast to int.
$c = (int)$a + (int)$b;  # Again, $c = int(9)

In the code you gave, $e and $n are initially assigned string values and never changed. Therefore, $e and $n are always of type string. It's just that PHP automatically typecasts them to integers for use in an integer context, such as mathematical operations. When you see a string used in a mathematical operation, you can mentally insert the explicit conversion to integer to get the meaning.

vacunita said:
Now because after all the processing the contents of the variables are still numeric, they can be used in mathematic operations.
Just as a point of interest for CADTenchy, in PHP you can use strings in mathematical operations whether they are numeric or not. Any string value can be implicitly converted to an integer. It's just that the result may not be meaningful. For example, the following are all perfectly legal in PHP:
Code:
$a = 1 + "car";    # Gives $a = int(1)
$b = "bus" - true; # Gives $b = int(-1)
$c = "8th Street" * "2nd Street";  # Gives $c = int(16)
So while this can be quite a handy feature, if you don't know what you're doing, it can also give you enough rope to hang yourself in some cases.
 
thanks both, that's really interesting and helpful.
If only there was the time to learn all the programming languages...

One final check on my original question please.

So if $e and $n start as strings and therefore infering integers if numbers, would the later line using $e:
Code:
$e=100000.0*($T1%5.0)+$e*$pr;
be the same as
Code:
$e=int( 100000.0*($T1%5.0)+$e*$pr );
(ignoring the fact that all the variables in this sum look like they are whole numbers anyway)


Steve (Delphi 2007 & XP)
 
i don't understand the bit 'therefore inferring integers if numbers' as a sequitur from the string comment. the fact that a variable may be stored as a string does not prevent php from inferring it as a decimal when it is time to convert it.

so discounting your inference, the two lines of code are not the same as line 2
(i) would not work (would throw a fatal error); and
(ii) even if you fixed it, the result MAY not be the same depending on the value of $T1 as you might be getting a decimal out of the top code.

to cast to an integer you would do this

Code:
(int) 100000.0*($T1%5.0)+$e*$pr;
//or
intval(100000.0*($T1%5.0)+$e*$pr);

the two lines would be equivalent if you cast instead to a float using (float) or floatval().

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top