Jump to content

[SOLVED] Need some quick help in PHP. :)


Recommended Posts

Guest Xanza

I usually do this kinda stuff where I find something interesting about PHP, and try to replicate it... And recently I got into here-doc - and I came up with this just to test it out and play with it:

 

<?php

class info{
var $name;
var $age;
var $group;

function info(){
	$this->name = "Hello, my name is {$name}, ";
	$this->age = "and I am {$age} year's old";
	$this->group = array("1","2","3","4","5");
	}
}
$info = new info();

echo <<<EOT
{$info->name}{$info->age}, and am in group {$info->group[1]}!
EOT;
?>

 

Then I friggin' realized that I know nothing about classes really... lol! I've been reading up in the php docs but I can't really find a solution to my question, which would be what's the easiest way to add an array to the other two variables that I have in this script: "name" and "age". Any help would be appreciated!

 

Thanks again,

    Xanza

Link to comment
https://forums.phpfreaks.com/topic/109362-solved-need-some-quick-help-in-php/
Share on other sites

Guest Xanza

That's what I originally had, but unfortunately it dident work.. :( Any specific reason why?

 

<?php

class info{
var $name = array("Jeff","Ducky","Zach");
var $age = array("18","19","20");
var $group;

function info(){
	$this->name = "Hello, my name is {$name}, ";
	$this->age = "and I am {$age} year's old";
	$this->group = array("1","2","3","4","5");
	}
}
$info = new info();

echo <<<EOT
{$info->name[0]}{$info->age[0]}, and am in group {$info->group[1]}!
EOT;
?>

 

And this outputs: http://xanza.info/test.php

Hmm... it seems your objective here is very strange. You are trying to instanitiate the object with an initiation function that messes with the variables, yet you want to echo a value from an array of which you have changed in the past. Your best idea would be to do the following:

 

<?php

class info
{
var $name   = array ("Jeff", "Ducky", "Zach");
var $age    = array ("18", "19", "20");
var $group  = array ("1", "2", "3", "4", "5");

function info($num)
{
	$this->name = "Hello, my name is ". $this->name[$num] .", ";
	$this->age = "and I am ". $this->age[$num] ." year's old";
	$this->group = $this->group[$num];
}
}

$info = array();
$info[0] = new info(0); // Object 1
$info[1] = new info(1); // Object 2

echo <<<EOT
{$info[0]->name}{$info[0]->age}, and am in group {$info[0]->group}!
EOT;
?>

You are kind of missing the point of a class. Classes allow you to use multiple functions and variables without having to set global values and keep passing variables from function to function.

<?php

class info{
var $name;
var $age;
var $group = array();

function info(){
	$part[] = "Hello, my name is ".$this->name.", ";
	$part[] = "and I am ".$this->age." year's old";
	$part[] = $this->group;
    return $part;
    }
}
// call the class
$info = new info();
// Set your variables
  $info->name = "John";
  $info->age = "32";
  $info->group = array(1,2,3,4,5);
// assign a variable to your class function
  $part = $info->info();

echo <<<EOT
{$part[0]}{$part[1]}, and am in group {$part[2][1]}!
EOT;
?>

 

Ray

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.