Why XSLT Transformation Output has trailing number? [duplicate] - php

My print_r($view) function yields:
View Object
(
[viewArray:View:private] => Array
(
[title] => Projet JDelage
)
)
1 <--------------
What does the "1" at the end mean? The PHP manual isn't very clear on how to parse the output of print_r.

You probably have echo print_r($view). Remove the echo construct. And... what need do you have to parse its output? There are certainly much better ways to solve your problem.

print_r called with one argument (or with its second argument set to false), will echo the representation of its parameter to stdout. If it does this, it returns TRUE. Thus if you echo print_r($foo) you will print the contents of foo, followed by a string representation of the return value (which is 1).

When using print_r to return, than than output/print the value, pass the 2nd parameter which defines return as true.
echo print_r($view, true);
This is useful if you want to save the results to a variable or concatenate with another string.
$var = 'The array is: ' . print_r($view, true);

Related

A character 1 after every value I get from the database using CodeIgniter [duplicate]

My print_r($view) function yields:
View Object
(
[viewArray:View:private] => Array
(
[title] => Projet JDelage
)
)
1 <--------------
What does the "1" at the end mean? The PHP manual isn't very clear on how to parse the output of print_r.
You probably have echo print_r($view). Remove the echo construct. And... what need do you have to parse its output? There are certainly much better ways to solve your problem.
print_r called with one argument (or with its second argument set to false), will echo the representation of its parameter to stdout. If it does this, it returns TRUE. Thus if you echo print_r($foo) you will print the contents of foo, followed by a string representation of the return value (which is 1).
When using print_r to return, than than output/print the value, pass the 2nd parameter which defines return as true.
echo print_r($view, true);
This is useful if you want to save the results to a variable or concatenate with another string.
$var = 'The array is: ' . print_r($view, true);

isset() returns true from a string variable accessed as an array with any key

I face a problem like this:
$area="Dhaka";
isset($area); //returns true which is OK
isset($area['division']); //returns true why?
// actually, any array key of area returns true
isset($area['ANY_KEY']);//this is my question 1
isset($area['division']['zilla');//now it returns false.
//as I know it should returns false but why previous one was true.
Now if I do this:
$area['division'] = "Dhaka";
isset($area); // returns true which is OK
isset($area['division']); // returns true it's also OK
isset($area['ANY_KEY']); // returns false. I also expect this
isset($area['division']['ANY_KEY']); // returns true why? question #2
Basically both of my questions are the same.
Can anyone explain this?
As with every programming language in existence, a string is stored as an array of characters.
If I did:
$area = "Dhaka";
echo $area[0];
It would return D.
I could also echo the whole string by doing:
echo $area[0].$area[1].$area[2].$area[3].$area[4];
PHP will also type juggle a string into 0 when passed in a manner that accepts only integers.
So by doing:
echo $area['division'];
You would essentially be doing:
echo $area[0];
and again, getting D.
That's why isset($area['division']) returns a true value.
Why doesn't $area['foo']['bar'] (aka $area[0][0]) work? Because $area is only a single-dimension array.
The best approach to handle this problem when you're working with a variable that could either be a string or an array is to test with is_array() before trying to treat your variable as an array:
is_array($area) && isset($area['division'])
PHP lets you treat a string as an array:
$foo = 'bar';
echo $foo[1]; // outputs 'a'
So
$area['division']
will be parsed/executed as
$area[0];
(the keys cannot be strings, since it's not REALLY an array, so PHP type-converts your division string by its convert-to-int rules, and gives 0), and evaluate to the letter D in Dhaka, which is obviously set.
Okay, here's a solution rather than explaining why isset isn't going to work properly.
You want to check if an array element is set based on it's index string. Here's how I might do it:
function isset_by_strkey($KeyStr,$Ar)
{
if(array_key_exists($KeyStr,$Ar))
{
if(strlen($Ar[$KeyStr]) > 0 || is_numeric($Ar[$KeyStr] !== FALSE)
{
return TRUE;
}
return FALSE;
}
}
isset_by_strkey('ANY_KEY',$area); // will return false if ANY_KEY is not set in $area array and true if it is.
The best way to access a linear array in php is
// string treated as an linear array
$string= "roni" ;
echo $string{0} . $string{1} . $string{2} . $string{3};
// output = roni
It is expected behaviour.
PHP Documentation covers this
You can try empty() instead.
If it is returning true for keys that do not exist there's nothing you can do; however, you can make sure that it doesn't have a negative effect on your code. Just use array_key_exists() and then perform isset() on the array element.
Edit: In fact, using array_key_exists() you shouldn't even need isset if it is misbehaving just use something like strlen() or check the value type if array_key_exists returns true.
The point is, rather than just saying isset($Ar['something']) do:
if(array_key_exists('something',$Ar) )
and if necessary check the value length or type. If you need to check the array exists before that of course use isset() or is_array() on just the array itself.

Parse error: syntax error, unexpected 'unset' (T_UNSET)

I am using simple php unset() function to remove and index of array but it's showing the following error:
Parse error: syntax error, unexpected 'unset' (T_UNSET)
Here is my erroneous code:
echo $totalArray = unset($linkExtHelp[0]);
Thanks in advance.
Try this, Reason for unset($linkExtHelp[0]) assigning to the variable echo $totalArray =
You can't assign the unset() value to the variable, You can use to check before unset and after unset as like below. In other words, unset does not have any return value, since unset is a void. Void - does not provide a result value to its caller.
Syntax: void unset ( mixed $var [, mixed $... ] )
echo "Before unset: ".$linkExtHelp[0];
unset($linkExtHelp[0]);
$linkExtHelp = array_values($linkExtHelp);
echo "After unset: ".$linkExtHelp[0];
instead of
echo $totalArray = unset($linkExtHelp[0]);
unset does not return a value - it cannot be used meaningfully as an expression, even if such a production was accepted.
However, the parse error is caused because unset is a keyword and a "special production": even though unset looks like a function, it is not a function1. As such, unset is only valid as a statement2 per the language grammar.
The production can be found in zend_language_parser.y:
309 unticked_statement:
| ..
338 | T_UNSET '(' unset_variables ')' ';'
1 The syntax is due to historical design choices, and arguably a mistake from a consistency viewpoint:
Note: Because [unset] is a language construct and not a function, it cannot be called using variable functions.
2 There is also "(unset) casting", but I'm ignoring that here.
Unset does not return anything, it is a keyword.
I assume you want to re-index the array, you can use array_values to do this
Try this:
unset($linkExtHelp[0]);
$totalArray = array_values($linkExtHelp);
You can't assign "unset". this is how I do.
you need to make a temp array.
$totalArray = $linkExtHelp; // assign it to a new array (so you can keep the original one )
foreach ($totalArray as $key => $value) {
unset($totalArray [$key]['save_day']); // if you need to remove all of 'save_day' value from multi array
}
var_dump($totalArray); // after unset
var_dump($linkExtHelp); // the original array
//echo $totalArray = unset($linkExtHelp[0]); // This is wrong. no echo
I hope it helps.

php var_dump equivelant or print_r

since I am using json, and ajax it's annoting I cn't pass the value on a valid json.
is there away to just return the value of var dump without it echo,being output to the browser.
e.g.
$data = 'my_data';
get_var_dump($data);//not real func
//should do nothing.
$get['data'] = get_var_dump($data);
$get['error']= false;
echo json_encode($get);
//should be something like
//{"data,"string(7) my_data","error":false}
or the print_r equivalent I just want to to be assigned to a var instead of outputting it.
or if ur a wordpress fan, difference between bloginfo('url'); and get_bloginfo('url'); should make it easier :)
Sure you can! To do that you will need two PHP buffer functions: ob_start and ob_get_clean. The first one starts buffering, when the second is getting value and cleaning buffer. Example:
ob_start();
var_dump($array);
$value = ob_get_clean();
print_r has the option for a second parameter. When set to true, it returns the dump as an array instead of displaying it.
http://us2.php.net/print_r
Check the var_export() function:
http://php.net/manual/en/function.var-export.php
You pass a variable to it and a second boolean parameter, if the second parameter is true the functions return a string with a rapresentation of the variable:
<?php
$a = array(1, 2);
$dump = var_export($a, true);
print $dump;
?>
$dump contains something like
array (
0 => 1,
1 => 2,
)
Everybody has beaten me to it: var_export()

What does "1" mean at the end of a php print_r statement?

My print_r($view) function yields:
View Object
(
[viewArray:View:private] => Array
(
[title] => Projet JDelage
)
)
1 <--------------
What does the "1" at the end mean? The PHP manual isn't very clear on how to parse the output of print_r.
You probably have echo print_r($view). Remove the echo construct. And... what need do you have to parse its output? There are certainly much better ways to solve your problem.
print_r called with one argument (or with its second argument set to false), will echo the representation of its parameter to stdout. If it does this, it returns TRUE. Thus if you echo print_r($foo) you will print the contents of foo, followed by a string representation of the return value (which is 1).
When using print_r to return, than than output/print the value, pass the 2nd parameter which defines return as true.
echo print_r($view, true);
This is useful if you want to save the results to a variable or concatenate with another string.
$var = 'The array is: ' . print_r($view, true);

Categories