Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

function errors

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

function errors

Postby Virtuoso » Thu Jul 24, 2003 9:03 am

Code: Select all
<?php
function le_mysql_connect()
{
   global $host,$database,$dbuser,$dbpassword;
   $connection = mysql_connect($host,$dbuser,$dbpassword);
   $db = mysql_select_db($database);
   return $db;
}
function le_visitor_info()
{
   global $_COOKIE['le_username'];
   $query_visitor = "SELECT * FROM members WHERE username='".$_COOKIE['le_username']."'";
   $result_visitor = mysql_query($query_visitor);
   $row_visitor = mysql_fetch_array($result_visitor);
   return $row_visitor;
}
function smilies()
{
   $query_smilies = "SELECT * FROM smilies";
   $result_smilies = mysql_query($query_smilies);
   $smilies_search = array();
   $smilies_replace = array();
   while ($row_replace = mysql_fetch_array($result_smilies))
   {
      $smilies_search[] = $row_replace['code'];
   
      $smilies_replace[] = '<img src="'.$row_replace['image_location'].'">';
   }
   return $smilies_search[];
   return $smilies_replace[];
}
?>


I get this error:
Code: Select all
Parse error: parse error, expecting `','' or `';'' in /home/virtuoso/public_html/post/functions.php on line 11


If I remove
Code: Select all
global $_COOKIE['le_username'];

Then I get:
Code: Select all
Fatal error: Cannot use [] for reading in /home/virtuoso/public_html/post/functions.php on line 27


I don't even know if I get the functions correct.
should I just write out the entire code instead of using functions. This is the first time I created my own function.
User avatar
Virtuoso
php-forum Active User
php-forum Active User
 
Posts: 268
Joined: Mon Jul 21, 2003 7:05 am

Re: function errors

Postby swirlee » Thu Jul 24, 2003 10:23 am

I'm not sure about the first one. The code looks fine around and before line 11. But there are two other problems, one of which is the cause of your second error.

Firstly, adding [] to the end of a variable name is an assignment construct. It tells PHP to add an element to the array whose name precedes it. It's kind of like the .= operator from PHP's point of view. When PHP sees [] after a variable name, it expects it to be followed by = (assignment operator) and an expression (anything which has a value, like a variable or a function with a return). If you're accessing an array, not adding to it, you should not and cannot use the [] suffix. So, to pass an array, just use the variable name and leave off the [].

I encourage you familiarize yourself with the documentation concering Expressions. It'll make your life a lot easier.

Second, your function definitions look approximately correct. The one problem is that a function cannot return more than one value. That is, a function may only use a single return statement. Once a function returns a value, it stops, so anything after your first return won't occur. If you need to return more than one value from a function, it's recommended that you return them as an array, and, if necessary, use list() to place them into variables outside the function. Refer to the Functions documentation (look a the Returning values section) for more information.

This last isn't so much a problem as an observation: Do you know how to pass a variable to a function as an argument? I ask because none of the functions above accept any arguments, but at least one, le_mysql_connect(), would certainly be better for it. It will save you the trouble of casting quite a lot of variables as globals. I get the feeling that you don't quite understand Variable scope, but use global instead just because it makes your code work. I'll warn you, in PHP5, understanding of scope will become far more important. Anyway, rather than try to explain passing variables to arguments to functions to you, as I'd surely do poorly, I'll refer you, as usual, to the documentation.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby Virtuoso » Thu Jul 24, 2003 10:57 am

Right now, im trying to focus my attention on the links you gave, but is there anything shorter that a lazy 14 year old kid would probably read?

I have a tendancy to do things the complicated way when there is an easier way also.

Ill really try to read and understand the stuff carefully (i also tend to skim through long and boring information and getting a vague idea)
User avatar
Virtuoso
php-forum Active User
php-forum Active User
 
Posts: 268
Joined: Mon Jul 21, 2003 7:05 am

Postby swirlee » Thu Jul 24, 2003 12:03 pm

I do apologize for my chronically lengthy explanations. If you read and understand the sections on Functions and Expressions, you should be fine. I understand the desire for (and utility of) skimming, but I've found that the PHP documentation is sometimes poorly organized (especially the function references) which makes getting a good idea of how things work by skimming a difficult proposition. I tend to look at the code examples first, and then if I see an example of something that looks like what I'm trying to do, I read the text above it.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back


Return to PHP coding => General

Who is online

Users browsing this forum: Exabot [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.