Jump to content

Recommended Posts

i am trying to pass an array to a function then echo it this is driving me nutty

 

i beleve this is an associative array not sure thow

 

array('d' => 'a', 'f' => 'g', 'h' => 'c')

 

this is what i have that doesnt work any help whold be aprischiated

<?php
function assign($var_array){
foreach ($var_array AS $var => $content){
echo $var $content;
}
}

assign( array('d' => 'a', 'f' => 'g', 'h' => 'c'));
?>

i get "dafghc" as my result (separate the variables with a period)

 

<?php
function assign($var_array)
{
    foreach ($var_array as $var => $content) {
        echo $var . $content;
    }
}

assign(array('d' => 'a', 'f' => 'g', 'h' => 'c'));
?>

i get "dafghc" as my result (separate the variables with a period)

 

<?php
function assign($var_array)
{
    foreach ($var_array as $var => $content) {
        echo $var . $content;
    }
}

assign(array('d' => 'a', 'f' => 'g', 'h' => 'c'));
?>

 

that fixed it but can you explain why thous  variables need to be seperated with a period it doesnt seem like it whould  be needed please forgive me i am quite new

The period is the concatenation operator.

 

You can not say

<?php
$a = "this is";
$b = "a string";
echo $a $b;
?>

You can do

<?php
$a = "this is";
$b = "a string";
echo $a . $b;
?>

or

<?php
$a = "this is";
$b = "a string";
echo "$a $b";
?>

or

<?php
$a = "this is";
$b = "a string";
echo $a . ' ' . $b;
?>

 

Ken

 

 

thanks for your help

i have another question not sure if i should start a new post or not

 

is it possible to pass a loop to another function to print it maybe my example will help

 


<?php
class test
{
var $testvar;

function assign($var_array,$cac)
{
foreach ($var_array AS $var => $content)
{
$this->testvar = $var . $content;
}

}

function output()
{
echo $this->testvar;
}
}
$tpl = new test();

$tpl->assign(array('d' => 'a',
	 'f' => 'g',
	 'h' => 'c')," mmmmn");

$tpl->output();
?>

http://www.php.net/manual/en/language.operators.string.php

 

String Operators

 

There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator ('.='), which appends the argument on the right side to the argument on the left side. Please read Assignment Operators for more information.

 

<?php
$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"

$a = "Hello ";
$a .= "World!";     // now $a contains "Hello World!"
?>

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.