Log in

View Full Version : Operators and "dots"



pcbrainbuster
03-05-2007, 10:51 PM
Hello all :),

As some of you most likely know I am a complete begginer to PHP.

I need some help understanding how operators and the dots for the echo works.

Here is an example of how I have read of doing it -

<?php
$txt1 = "Hello";
$txt2 = "Fool";
echo $txt1." ".$txt2;
?>

But I read about operators in JavaScript and completely understand it, can you replace the dots above with + like this -

<?php
$txt1 = "Hello";
$txt2 = "Fool";
echo $txt1 + " " + $txt2;
?>

Thanks !:)

Blake
03-05-2007, 10:57 PM
No, that won't work. The dot in php (when dealing with strings) does the same thing as the plus in JavaScript.

mburt
03-05-2007, 11:03 PM
No, the + operator in PHP would try to parse the character as an operator not an assigment to a string.
It's like saying (mathematically)
Hello + Fool = ?

That's where PHP messes up, so fortunately they have a way around it :). The genius' at PHP decided to use a different character, and as Blake said, the "." sign.

Twey
03-05-2007, 11:06 PM
No. Javascript overloads the + operator to mean both addition (of numbers) and concatenation (of strings). However, PHP has two separate operators, + for addition and . for concatenation. The two are not interchangeable.

mburt
03-05-2007, 11:18 PM
No. Javascript overloads the + operator to mean both addition (of numbers) and concatenation (of strings).
Which I think is more confusing in the long run, and less effecient. Having to use String() all the time to convert numbers to strings is just a nussiance (spelling?).

Twey
03-05-2007, 11:20 PM
Hmm? One very rarely has to convert numbers to strings in JS, and certainly not for the purposes of concatenation.

mburt
03-05-2007, 11:26 PM
In a script I used a while back I used setAttribute, and IE didn't like numbers in the function:

setAttribute("src",num+".gif");

so I did

setAttribute("src",String(num)+".gif";

Twey
03-05-2007, 11:58 PM
That should make no difference at all... num should be type-coerced into a string.

mburt
03-06-2007, 12:06 AM
Well, it didn't work before String() and did after.

pcbrainbuster
03-06-2007, 08:12 AM
LOL operator....operator, here is my order :),

But anyway mburt here is an example of + in JavaScript -

<script type="text/javascript">
var txt1 = "Hello"
var txt2 = 123
document.write(txt1+" "+txt2)
</script>

Anyway i once read that the default language for php in vbscript and i also read that javascript can replace that - please tell me how :)
(i may be mistaking this with another server language)

Twey
03-06-2007, 02:42 PM
You are. VBScript is an ASP language. The default (and only) language used by the PHP parser is PHP.

pcbrainbuster
03-06-2007, 04:43 PM
Hmmm I see - thanks :),

But can you please give me a php script that will display on the site of how many people have visited the site ?

Thanks :)

mburt
03-06-2007, 04:51 PM
I assume you posted in the wrong thread. Please stop asking this question, and go to the original thread (http://www.dynamicdrive.com/forums/showthread.php?t=18046).

pcbrainbuster
03-06-2007, 09:05 PM
Thanks and sorry :)

pcbrainbuster
03-07-2007, 08:38 PM
Please may I have an example ?