ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hey everyone, I have no idea what I'm doing wrong
and I feel really stupid, have looked at php's manual
but don't know what to do.
In the code below, if you uncomment the line in the
function setRow(), it gets stuck in an infinite loop. As
it turns out, my assignment to what is supposed to be
one of the member variables (an array, $data), is actually
assigning to one of the other member variables ($numRows).
WTF is that?!!!
I'm a c(++) background, so this syntax is kinda weird to
me (having to put $this before everything, so annoying!).
The program's supposed to check if the (2-d) array is big enough,
and if not add enough rows (however many user wants). Can
someone please correct my php? Thanks.
PHP Code:
<?php // classes for making and printing tables
class cell { public $rspan, $cspan, $data;
public function __construct() { $argc= func_num_args(); $args= func_get_args();
// set a row's cells to the given array public function setRow($r, $arr) { print 'r: '. print_r($r, TRUE) ."\n"; print 'arr: '. print_r($arr, TRUE) ."\n"; print 'num rows: '. print_r($this->$numRows, TRUE) ."\n\n\n";
count(arr$) is a function that is being re-evaluated each time through the loop. You are adding a cell each time through the loop, so count(arr$) increases each time through. Hence you never reach the end.
Replace the function with a constant and then I'll bet it works.
Thanks, but $arr isn't being changed there, what's being
changed is $this->$data[$r][0] ..... $this->$data[$r][n-1], where
n is the length of $arr. But $arr is still constant in that function.
For example, this does the same thing:
PHP Code:
for($i= 0, $j= count($arr); $i < $j; $i++) $this->$data[$r][$i]= new cell($arr[$i]); // PROBLEM HERE
That was the spot you're talking about right?
Anyway, does this have something to do with the variable being
allocated and then overwritten unintentionally (has anyone seen
that before in php) or am I just messing it up somewhere?
Wrote a little more but haven't figured it out yet.
It no longer gets in an infinite loop, but for some reason
$this->$data never gets made into an array, so
$this->$data[n] can never get made into arrays themselves.
Probably some stupid syntax mistake I have, anyone see
what it is? Thanx
PHP Code:
<?php // classes for making and printing tables
class cell { public $rspan, $cspan, $data;
public function __construct() { $argc= func_num_args(); $args= func_get_args();
// set a row's cells to the given array public function setRow($r, $arr) { print 'r: '. print_r($r, TRUE) ."\n"; print 'arr: '. print_r($arr, TRUE) ."\n"; print 'num rows: '. print_r($this->$numRows, TRUE) ."\n\n\n";
Okay, I made an example script just to see if I'm doing my
php syntax right, but I'm not, and php.net and tutorials
aren't really helping.
If anyone can explain what I'm doing wrong here, I'd really
appreciate it. And if anyone can help me with the other, bigger
program (one I'm really working on) that would be great too.
Also anyone know why print_r doesn't print the class members
when given a class? My own tostr() func says the members
are set to a number (wrong number though), but print_r
lists that number in private, what's that about?
Here's my version:
Code:
PHP 5.0.4 (cli) (built: Mar 31 2005 02:45:48)
Copyright (c) 1997-2004 The PHP Group
Zend Engine v2.0.4-dev, Copyright (c) 1998-2004 Zend Technologies
Here's the code I can't get working:
PHP Code:
<?php
// example class class point { public $x, $y;
public function __construct($X= 0, $Y= 0) { $this->$x= $X; $this->$y= $Y; }
public function add($a) // add a point or scalar { $ret= new point();
If you have the chance, could you point at a section of code in your second example, or first that this revelation applies to? Thanks much for sharing your solution.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.