473,326 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

variables type

It's a newbie question...

Is it possible to type variable in PHP inspite this is a loose type
language. Or would that be required in some cases?
Apr 11 '06 #1
7 1251
"Tijs Verwest" <st*************@bluewin.ch> wrote in message
news:44**********@news.bluewin.ch...
It's a newbie question...

Is it possible to type variable in PHP inspite this is a loose type
language.

Variables do have some sort of typing but as you know it's very loose. if a
particular variable happens to be an integer and you assign an array to it,
it becomes an array. There are ways of detecting the type of a variable and
there are ways of casting a variable to certain type.

A little exaple of ways of manipulating the type.
<?php

$foo; // Now we have no idea what this is.
$foo = "bar"; // assigning a string to it makes it a string.
echo gettype($foo); // this prints the type. string
$bar = (int)$foo; // now we assign the string into $bar but typecast it to
int.
if(is_int($bar)) echo "$bar is an integer"; // is it int?
else echo "wtf? $bar is not an int, it's ". get_type($bar);

?>

I recommend you to read this: http://fi2.php.net/manual/en/ref.var.php
Or would that be required in some cases?


It depends on your code. Sometimes it's crucial, sometimes not. If and when
it is, I use type casts to make sure something is of some type, for example
when inserting a number intoa database I make sure it's a number to avoid
errors. A simple type cast takes care of it.

--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
Apr 12 '06 #2
Tijs Verwest wrote:
It's a newbie question...

Is it possible to type variable in PHP inspite this is a loose type
language. Or would that be required in some cases?


Hi,

PHP has the concept of types, however:

From manual:
-----------------
The type of a variable is usually not set by the programmer; rather, it is
decided at runtime by PHP depending on the context in which that variable
is used.
-----------------

Read more here:
http://nl2.php.net/manual/en/language.types.php

Also be sure to follow the link to 'Type Juggling'.

So, unlike strongtyped languages, in PHP you let the interpretation of the
type of a variable to the language itself.
In case of doubt you can ALWAYS cast to the type you want/demand.

for example:
$myInt = (int)$unclearValue;

You can also test for types, but that is all in the documentation.

In my experience, you do need the casting sometimes, but once you get
familiar with PHP it is easy to spot where.
If in doubt: just cast.

Good luck,
Erwin Moller
Apr 12 '06 #3
I use a notation, whose name I can't remember, to show what type the
variable should be.

$iInt
$aArray
$bBool
$mMixed - could be anything
$sString
$oObj

so I can do loops like
while(list($sFieldName,$aFieldDef)=each($aFields)) {}

Also, some regulalry used objects are given their own prefix as opposed
to a general 'o'

Checking function declarations becomes much easier also.

Apr 12 '06 #4
fletch wrote:
I use a notation, whose name I can't remember, to show what type the
variable should be.

$iInt
$aArray
$bBool
$mMixed - could be anything
$sString
$oObj

so I can do loops like
while(list($sFieldName,$aFieldDef)=each($aFields)) {}

Also, some regulalry used objects are given their own prefix as opposed
to a general 'o'

Checking function declarations becomes much easier also.


Hi,

I think that is called Hongarian Notation.
Not sure either. :-)

Erwin

Apr 12 '06 #5
Erwin Moller wrote:
fletch wrote:

I use a notation, whose name I can't remember, to show what type the
variable should be.

$iInt
$aArray
$bBool
$mMixed - could be anything
$sString
$oObj

so I can do loops like
while(list($sFieldName,$aFieldDef)=each($aFields )){}

Also, some regulalry used objects are given their own prefix as opposed
to a general 'o'

Checking function declarations becomes much easier also.

Hi,

I think that is called Hongarian Notation.
Not sure either. :-)

Erwin


Close. It's "Hungarian". :-)
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 12 '06 #6
Jerry Stuckle wrote:
Erwin Moller wrote:
fletch wrote:

I use a notation, whose name I can't remember, to show what type the
variable should be.

$iInt
$aArray
$bBool
$mMixed - could be anything
$sString
$oObj

so I can do loops like
while(list($sFieldName,$aFieldDef)=each($aField s)){}

Also, some regulalry used objects are given their own prefix as opposed
to a general 'o'

Checking function declarations becomes much easier also.

Hi,

I think that is called Hongarian Notation.
Not sure either. :-)

Erwin


Close. It's "Hungarian". :-)


:-)

Ok, thx.
It's not easy being Dutch on the net. ;-)

Regards,
Erwin Moller

Apr 12 '06 #7
Erwin Moller wrote:
Ok, thx.
It's not easy being Dutch on the net. ;-)

Regards,
Erwin Moller


:-)

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Apr 12 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Dave | last post by:
Hello, I'm a somewhat PHP newbie, so please bear with me. Here is what I am trying to do: $foobar = "blah blah"; $a = "foo"; $b = "bar"; $fooboo = "$a" . "$b"; echo $fooboo
7
by: BCC | last post by:
Hi, I have a class with several member variables that should be initialized according to user input before an object is instantiated. Using a static variable is required here. But, I would...
5
by: Phil Grimpo | last post by:
I have a very odd situation here. I have an administration page, where based on a users permissions, a recordset is called from the SQL server which has a list of paths to "Module Menus". Each of...
5
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example...
122
by: Einar | last post by:
Hi, I wonder if there is a nice bit twiddling hack to compare a large number of variables? If you first store them in an array, you can do: for (i = 0; i < n; i++) { if (array != value) {...
30
by: Javaman59 | last post by:
I come from a background of Ada and C++ programming, where it was considered good practice to explicitly initialize every variable. Now that I'm programming in C# I think that it would be best...
17
by: yb | last post by:
Hi, Looking for clarification of undefined variables vs. error in JavaScript code. e.g. <script> alert( z ); // this will be an error, i.e. an exception </script>
1
by: Paradox | last post by:
Hi everyone, kinda new to Javascript. Here is the issue, I have a form that sends to a different action based on the button pushed. But it also is sending variables to the server as well. Here...
6
by: Chris Carlen | last post by:
Hi: I have an embedded system, platform: TI TMS320F2812 32-bit integer DSP. A module (.c file) contains external (to the funcs in that module) variables which are used to affect the operation...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.