No Quotes Around numbers?

Hi,

I don’t remember reading in any php tutorial that we should not use quotes around numbers.
I am reading codeacademy.com now and it says:

Remember: no quotes around numbers!


**<?php $myName = "Eric";**
**      $myAge = 26; ?>**

But the followings work with both types of quotes:

<!DOCTYPE html>

<html>
    <head>
        <link type='text/css' rel='stylesheet' href='style.css'/>
		<title>PHP FTW!</title>
	</head>
	<body>
        <!-- Write your PHP code below!-->
        <p>
            <?php $myAge = '16'; 
			echo $myAge;
			?>
        </p>   
	</body>
</html>
<!DOCTYPE html>

<html>
    <head>
        <link type='text/css' rel='stylesheet' href='style.css'/>
		<title>PHP FTW!</title>
	</head>
	<body>
        <!-- Write your PHP code below!-->
        <p>
            <?php $myAge = "16"; 
			echo $myAge;
			?>
        </p>   
	</body>
</html>

Correct. If you put quotes around it, it’s no longer a number and becomes a string.

2 Likes

Meaning, it no longer remains an INTEGER and becomes a STRING (Sentence). Gets counted as a sentence (STRING) even if it’s just a single word (eg 1. “16”, eg 2. ‘16’). Right ?

But, I thought if you use a single quote then it becomes a string. I can understand this a string: ‘16’.
But this a string ? … : “16”.
If that is not a string then shouldn’t double quotes be allowed around numbers/INTEGERS ?

Same either way.

One other thing to remember, that PHP has Type Juggling and it has String Conversion built-in.

So the following can be done:

<?php
$foo = "1";  // $foo is string (ASCII 49)
echo $foo . "<br>";
$foo *= 2;   // $foo is now an integer (2)
echo $foo . "<br>";
$foo = $foo * 1.3;  // $foo is now a float (2.6)
echo $foo . "<br>";

PHP sees you are trying to do multiplication on Line 2, so it converts “1” to be an integer (ie: $foo = 1;), then does the multiplication. As such, when that multiplication is complete, the type of variable $foo is, is now an integer instead of a string.

On Line 3, it sees you are mulitplying by a float/decimal, and as such, the type of $foo changes again, this time to a float.

3 Likes

This looks confusing:
$foo *= 2; // $foo is now an integer (2)

Are you sure the asterisk is at the right place ? Maybe you mis-typed in haste ?
2 things I learnt now, thanks to you and codeacademy.com (or I re-learnt them now after I forgot about them as I forget complicated or confusing things things very quick) is that:

1. Not only single quoted phrases are considered strings but double quoted ones too.
2. String is not a sentence/phrase (more than one word) but a word (even a single word).

PS - I am teaching my php.exe bot all this now so it will remind me all these when I forget them again.

Continuing the lesson on another tab in Chrome.

Cheers!

No, it isn’t a typo, think of *= as a shortcut operator. Instead of writing

$foo = $foo * 2;

I can write:

$foo *= 2;

The value of $foo after either approach, is the same, the value is doubled.

2 Likes

Check out this page around 1/3 of the way down in the comments

http://php.net/manual/en/language.operators.assignment.php

3 Likes

And $var = "2dasdasd" can also be treated like a integer, because it starts with integer, this is called numerical string, but $var = "dasdasd2" can’t be integer its string.

1 Like

Thanks. But, I got confused on the bit after the comma:

The value of $foo after either approach, is the same, the value is doubled.

Value is same or doubled ?
I understood the short-cut bit.

I don’t think that’s quite correct. It has nothing to do with words, as such, but with a sequence of characters. So “k1 p1 psso” is not a word or a sentence, but it is a string, as is “yrn”.

2 Likes

“Numerical String” ?!!! Haven’t come across that yet but I guess I will soon. :slight_smile:
Thanks for mentioning it.

Do you mind elaborating more ?
Because I thought, a “string” is a sentence with alphabetical characters.
Today, I’m thinking it’s a word or words with alphabetical characters. Symbols and numbers don’t count.
That is my definition of a String so far. Unless, you say otherwise, ofcourse.
I have read on Strings many times on many tutorial sites but that is what I have understood so far.

String can hold any sequence of characters that are surrounded by quotes.

2 Likes

Let me rephrae.

The end results after running each line is the same, the value that $foo originally held is doubled and placed back into $foo.

1 Like

I came across these funny operations before but they confused me and I skipped them. Now, it seems I’m gonna have to face them. Hard to remember these things and so gonna get my php.exe bot to remember it. Because, I know I will forget it.
Forget things like this:


<?php

$a = 3;
**$a += 5; // sets $a to 8, as if we had said: $a = $a + 5;**
$b = "Hello ";
$b .= "There!"; // sets $b to "Hello There!", just like $b = $b . "There!";

?>

http://php.net/manual/en/language.operators.assignment.php

It’s the 2nd line that is confusing and hard to remember. I always was crap at maths. Was better in Science and even Engineering Materials which the whole class found hard (Engineering Materials).

This is what I am reading now about STRINGS:

> A string is a word or phrase between quotes, like so: “Hello, world!”

> You can type a string all at once, like this:

> <?php_ _> echo "Hello, world!";_ _> ?>
> Or use the concatenation operator, which glues several strings together:

> <?php_ _> echo "Hello," . " " . "world" . "!";_ _> ?>
> The concatenation operator is just a dot (.).

https://www.codecademy.com/courses/web-beginner-en-StaFQ/1/2?curriculum_id=5124ef4c78d510dd89003eb8

You see, whenever I can’t remember a subject, I just make notes of it to refer to the notes when I come across the subject. Subjects like the shortcut assignment operator:

$a += 5;

And the Concatenation Operator format.

But, since you people are here and in the mood to answer questions then I’m gonna ask as much as I can now. No matter, how stupid they may sound. Not gonna feel shy or embarrassed to ask.
And so, here goes:

Q. Every tutorial mentions to use the dot to join phrases or sentences.

And so, if I want to join “Hello” and “World” and “!”, then I should do it like this (according to me):
echo “Hello,” . “world” . “!”;

But the tutorial shows it to do it like this:

echo “Hello,” . " " . “world” . “!”;

Where do the extra 2 double quotes and the extra dot come from (that I high-lighted) ?
I don’t understand these 2:

" " .

The two double-quotes have a space between them, to separate world from hello.

2 Likes

Thanks!
I noted it like this: “NOTE: The 2 double quotes represent the space in-between “Hello” and “World”.”

On my experiment, I noticed that whenever I delete the spaces inbetween the dots, the result is the same. Both of the following echo the same (in terms of spaces shown in-between words):

echo “Hello,”. " " .“world”.“!”;

echo “Hello,” . " " . “world” . “!”;

The echo statement is returning those elements which you have told it are part of the string. In other words, those parts which are enclosed within the quotes. That’s why you need to indicate the intended space with " ", because spaces outwith the quotation marks are just ignored as not being part of the string.

1 Like