Jump to content

Simple associative array syntax question


doug

Recommended Posts

In the tutorial, a session variable is set like in the following example:$_SESSION['views'] = 1;So $_SESSION is an associative array, the index is the string 'views' and the value is 1.In JavaScript associative arrays, the following two syntaxes are completely equivalent:$_SESSION['views']and$_SESSION.viewsIs there anything analogous in PHP - some dotted property syntax?Thanks,doug

Link to comment
Share on other sites

No, in PHP classes and arrays are two different things. In Javascript they are pretty much the same thing. Also, in PHP the member reference operator for a class is "->", the dot operator is for string concatenation.

Link to comment
Share on other sites

No, in PHP classes and arrays are two different things. In Javascript they are pretty much the same thing. Also, in PHP the member reference operator for a class is "->", the dot operator is for string concatenation.
In analogous JavaScript terms is a "class" like an object and a "member" like a property?doug
Link to comment
Share on other sites

A class is pretty much a definition of an object, the object is the defined instance of the class. Members are both methods and properties. Here's the class and object section of the manual:http://www.php.net/manual/en/language.oop5.php
One thing I couldn't find there, or maybe it was there but using different terminology than I am used to, is is there some way to have a dynamic property name?In this statement:$thisObject->xxx;xxx is a fixed identifier.Is it possible to do something like$thisObject->$xxxwhere the value of $xxx is the name of the property?By the by, my ISP is using PHP 4.4.4 instead of 5. Is that common?Thanks,doug
Link to comment
Share on other sites

Yes you can do that, assuming the variable holds a valid property name. You can also use one variable to access another one:

$foo = 'bar';$bar = 123;$$foo = $$foo * 2;echo $bar; // print 246

Some hosts still have PHP4, but it is no longer officially supported. PHP5 is already around 4 years old, so hosts should be moving there. It has quite a few improvements over PHP4 (the object model being one of them).

Link to comment
Share on other sites

Yes you can do that, assuming the variable holds a valid property name. You can also use one variable to access another one:
$foo = 'bar';$bar = 123;$$foo = $$foo * 2;echo $bar; // print 246

Some hosts still have PHP4, but it is no longer officially supported. PHP5 is already around 4 years old, so hosts should be moving there. It has quite a few improvements over PHP4 (the object model being one of them).

That is quite cool syntax. And that would be difficult to do so compactly without that $ delimiter at the head of each variable name.I'll inquire as to why my provider - which is a big provider - is still using the older version.Thanks again.doug
Link to comment
Share on other sites

PHP5 has a better OOPS support, as justsomeguy has stated, and it also supports the mysql Improved functions.PHP6 is already running around in the wild, so expect that it will be a short time before it gets to be common. Larry Ullman already has a book written for it.http://www.dmcinsights.com/phpmysql3/I haven't studied it yet, so I don't have many details or particulars.

Link to comment
Share on other sites

I'll inquire as to why my provider - which is a big provider - is still using the older version.
Now there you have the answer to your quesiton.PHP5 eliminates and changes some things from PHP4. The PHP manaual contains a whole section about migrating from PHP4 to PHP5. Since your host is a big provider, they already have a lot of customers running on PHP4.If they migrate to PHP5, this means a lot of unhappy customers. From a client's point of view, this is a needless, and what's worse, forced and unexpected, change that forces them to ceise business and call their web developer. From a web developer's point of view, especially if they have a lot of customers hosted on that host, this means a lot of whining and a lot of sites to fix.What's worse is that, unlike browser differences, where you can use another browser and the problem will go away, a server "break"age would truly break any browser.Until your host is able to run PHP4 and PHP5 simultaniously (so that they can host PHP for all new customers and/or everyone who wants PHP5), or is able to provide a new, PHP5 only hosting plan, they'll be stuck with PHP4.
Link to comment
Share on other sites

Now there you have the answer to your quesiton.PHP5 eliminates and changes some things from PHP4. The PHP manaual contains a whole section about migrating from PHP4 to PHP5. Since your host is a big provider, they already have a lot of customers running on PHP4.If they migrate to PHP5, this means a lot of unhappy customers. From a client's point of view, this is a needless, and what's worse, forced and unexpected, change that forces them to ceise business and call their web developer. From a web developer's point of view, especially if they have a lot of customers hosted on that host, this means a lot of whining and a lot of sites to fix.What's worse is that, unlike browser differences, where you can use another browser and the problem will go away, a server "break"age would truly break any browser.Until your host is able to run PHP4 and PHP5 simultaniously (so that they can host PHP for all new customers and/or everyone who wants PHP5), or is able to provide a new, PHP5 only hosting plan, they'll be stuck with PHP4.
It turned out when I asked that they are, in fact, able to host both simultaneously.If you add the following line to the .htaccess file:AddHandler application/x-httpd-php5 .phpthen PHP becomes version 5.2.2.All my test scripts so far continue to work. So I might as well continue to learn in version 5, right?doug
Link to comment
Share on other sites

It turned out when I asked that they are, in fact, able to host both simultaneously.If you add the following line to the .htaccess file:AddHandler application/x-httpd-php5 .phpthen PHP becomes version 5.2.2.All my test scripts so far continue to work. So I might as well continue to learn in version 5, right?doug
Wow. Great for them. And yes, you should continue in PHP5.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...