0

I've just decided to venture off into PHP land for fun and to learn, reading that php is loosely typed and that $var's can be reused is this true that the code below will pose no problem?

$x = 996;
$x = mysql_query("SELECT aString FROM table1");

the variable x will stored as an int datatype with 996, then after the second line it will stored as a string datatype with the string from the query?

There wont be any casting errors?

1
  • On a side note, mysql_query doesn't return a string. It returns a MySQL Result ID which is generally an integer that refers to a particular cached result set. Commented Aug 6, 2009 at 21:55

3 Answers 3

10

You are correct; that's the definition of being "loosely typed". However, that may not be the best practice.

http://drupaler.co.uk/blog/baby-dont-you-loose-your-type-me/66 is a good read on your subject.

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly- using the same variable name just confuses people that have to read the code.
3

There will be no errors, except that the second line won't give you a string, mysql_query returns an internal PHP type called a resource (generally some kind of opaque handle/pointer for library functions)

Comments

1

There wont be any casting errors?

Correct!

jalf explained it well here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.