Value placed in "define()" is not displayed correctly

Asked

Viewed 50 times

0

Well personal, I am writing a system of "versioning", I have a class that collects and compares some information. In index of my project I pass the current version of the system and in the configuration file I define the version key.

public function getEncodeVersion()
{
    return "0x".strtoupper(dechex(crc32($this->version)));
}

This function collects the current version.

In my definition file, I have the following define():

define("updateKeyReview", 0x133B6236);  //sem Aspas

I recover and compare this value in this function

public function compareVersion()
{
    if(constant("updateKeyReview") != $this->getEncodeVersion())
        $this->getMessageUpdate();
}

In theory it should work perfectly, because it’s just a silly comparison, but I got the updated version message, until I gave a echo in my define(), instead of it returning the value I am passing on it which is 0x133B6236, he’s returning 322658870, as if it had converted the value I took in the define(), but if I put like this:

define("updateKeyReview", "0x133B6236"); //com Aspas

It works normally, I already know it’s working that way, I just wanted to understand what happens and why the value is converted.

  • The PHP interprets the value 0x133B6236 (unquoted) as hexadecimal code, so it is converting the value. With the use of quotation marks, you make it explicit that the value is a string and that the PHP shall not convert him.

  • I understand Valdeir and thank you for the comment. However, in version 5.6 of PHP I do not have this problem, and reading the PHP patch Notes, I saw no change in the DEFINE function, which would bring the current result. Even so, thank you for your attention.

  • 1

    This does not cause the function define. If you run the code echo 0x133B6236;, the exit will be 322658870. To the PHP, all value started by 0x or 0X (and without quotation marks) is considered a hexadecimal code and is therefore interpreted and converted. https://ideone.com/Q4XZ6O

  • Test in other versions of PHP: http://sandbox.onlinephpfunctions.com/code/6599b0929bd2f9288353b4c9034e07e64e25be86

  • Really, I tested and the result was the same in all versions. However I have used this way that I did before and I had no mistake. Even so, thank you very much and Valew for your attention.

  • @Wagnercorrêarosa The answer solved your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

Show 1 more comment

1 answer

1

What are numbers

There is one thing that is the number, something countable, something that exists in nature, and therefore exists in the computer through electricity, and there is a numerical representation that we humans create to facilitate our understanding, not least because we don’t deal well with what we don’t see. So we create a textual representation for numbers, we create the one of the 0, 1, 2, etc. that we deal with every day, and we don’t even realize that it’s just texts (which are just drawings) that we understand as numbers.

On the computer when we see the number on the screen it’s just a text, always. It’s never a number, especially in the syntax of the language when we write a code. And no matter if it’s there in decimal, hexadecimal, binary or other notation, it’s just textual representations.

But there is a difference in syntax that we use to say when we want it to be treated as a number and when we want it to be treated as a text. It was agreed that if you write a sequence of numerical digits in simple form is a number and the computer will handle it, it can do calculations with that. In some languages it has also been agreed that it is possible to use a different decimal notation, for example if you have a prefix 0x means it will be hexadecimal, and even so some letters may be used in the number.

If you want to print this number on the screen will be incomprehensible directly, it is represented on the computer in a way that the human being does not understand, and even a direct conversion to text will appear something very strange. The solution is to do accounts to transform this number into characters that represent this number in a way that the human being understands. As there is a function ready to deal with it we do not even stop to think that this conversion is taking place. And again, no matter if the representation is decimal or not, each chooses as he wants, only the pattern is decimal.

When we put a number in quotes it’s just a text that just happens to have numeric digits, nothing else. You cannot compute with it, for the computer it is not a fact number, only a sequence of characters (actually going deeper is a sequence of numbers that are used through a table of drawn characters.

So if I use a number and I want to write a text directly in theory and in practice it will work because of everything I’ve written, and decimally nobody notices any problem. In hexadecimal you can notice a problem because the standard is to write in decimal, then what you wrote in hexadecimal code will not be respected, unless ask to write in hexadecimal format.

Reinforcement: decimal or hexadecimal are only two different ways of representing a textually number.

Then you need to decide if this is a description, if it is should use a text as well as CPF and telephone which seem to be numbers but are texts. Or if it is a number, we need to do calculus. If it is the first one becomes easy and everything works, if it is the second one you have to take care to have printed in hexadecimal notation, if that’s what you want, then it works.

You have to, first of all, conceptualize right what you’re doing.

define()

It’s not the problem of define() and I doubt that there is any difference between the versions of PHP, if you have, show evidence of this, you will eventually discover that some error occurred between one version and another.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.