Laravel: does exists a equivalent of var_export($var, True) helper? - php

Often becomes handy to debug out the dump of a variable
Laravel's
dd($v);
dump($v);
are so useful.
I wonder if there's such a function that dumps the variable as a returned string instead of printing it out.
Like in php :
var_export($var, TRUE)
Note: I'm asking for a specific Laravel function not the built in PHP function , var_dump, var_export or print_r. I already know about it.

You can use the pre tag to format your output
echo "<pre>";
print_r($data);
echo "</pre>";
Edited
If you want to store the dump in a variable, I guess you can do like below
Import use Illuminate\Support\Debug\Dumper;
$data = User::all(); // change it according to your requirement
array_map(function ($data) {
(new Dumper)->dump($data);
}, func_get_args());

I couldn't find anything, so I resort to this:
$string = var_export($data, true);
Log::info($string);

Related

On echo return string, on print_r return array or object in PHP

Some functions I have used return a string on echo, something like this.
echo my_function();
This prints "hello" on the screen.
print_r( my_function() );
This prints an array or an object on the screen. The function is the same in both cases, even the possible in parameters.
How is this done?
The possible function
function my_function() {
// If this function is used by echo return the string
// If this function is used by print_r return an array
$string = 'Hello';
$array = array('some', 'data');
// return $string or $array depending on
}
Real life example
This function return an image on echo and an object on print_r: http://getkirby.com/docs/cheatsheet/helpers/thumb
Your my_function() is not returning anything. So calling it in echo or print_r makes no diffrence. If you return string form the function and you want to just display it then you can echo it out. But if you return array from function and echo it then it will give you a unusful output. On the other hand if you print_r the function and function return string, then it will output just like echo do. But if you return array and call with print_r then it will show the array more readable. It is useful for debugging.
Here is more about echo and print_r()
echo
Outputs one or more strings separated by commas
No return value
print_r()
Outputs a human-readable representation of any one value
Accepts not just strings but other types including arrays and objects, formatting them to be readable
Useful when debugging
May return its output as a return value (instead of echoing) if the second optional argument is given
Functions should never depend on who they called, but how they are called.
There is no sense in for example creating a function that does addition on echo and subtraction on print. That is confusing.
try to use parameters as suggested in the question's comments.

PHP - Is concatenation possible between echo and print_r

In PHP, is it possible to write out an array within an echo command (or vice-versa)?
Thank you!
Sure, print_r() has an extra option to allow it to return the formatted text, instead of directly outputting it:
echo print_r($array, true);
not sure why you'd want to but you could possibly do
$var = print_r($array, true);
echo 'stack ' . $var;
to get the result your looking for
echo print_r($myVariable, true);
The second param signals the function to return the output rather than a boolean of success.

Instead of foreach loop, how can I use in_array()?

In codeigniter, in my controller file, I fetch data from model. To validate the results I use foreach loop and there is no problem. But instead of using foreach I want to use in_array() to control if variable is in database result array or not . Here is code :
function _remap($method,$params = array()){
//in construct, I use $this->model
if (isset($params[0])) {
$this->db->distinct('*****');
$this->db->from('*****');
$this->db->where('****',****);
$query = $this->db->get();
if (in_array($params[0],$query->results())) {
echo "in_array works";
}
}
But not echoing anything. How can I do it?Thanks.
According to the manual you could use result_array(). Otherwise you will get back an object. Of course you cannot use in_array for an object.
Also according to the manual you could change your code to
if (in_array($params[0], $query->first_row('array')) {
// ...
The in_array is just to be used for only checking a value in the value-list
e.g. array("Mac", "NT", "Irix", "Linux");
in_array wont work for your situation, because it returns object array.
I would suggest to write a helper method, that does the job you want and returns True or False. Information on how to do it can be found here

putting print_r results in variable

In PHP I can say:
print_r($var);
What I can't do (at least I don't know how) is:
$var_info = print_r($var);
How can I accomplish the effect of putting the results of a print_r inside a variable?
PHP v5.3.5
When the second parameter is set to
TRUE, print_r() will return the
information rather than print it
$var_info = print_r($var,true);
$var_info = print_r($var, true);

How to store all in a variable instead of echoing everything separately?

please see: http://pastebin.com/5za3uCi1
I'm quite new to php and I'm editing the ventrilo status script. What I'd like it to do is that it stores everything in one big variable for easy parsing instead of using separate echo's. Can someone tell me how I can accomplish this?
Thanks,
Dennis
You can use the output buffer and get the contents of it:
ob_start();
echo 'foobar';
$contents = ob_get_contents(); // now contains 'foobar'
ob_end_clean();
declare a variable at the beginning, say $data or whatever. then, replace the echo calls:
echo "hello";
with this:
$data .= "hello";
then return the $data variable at the end of the function.
Instead of the echo, you can use a simple affectation :
$request = "CVentriloStatus->Request() failed. <strong>$stat->m_error</strong><br><br>\n";
But you'll soon have issues to manage multiple variables.
You could create an object to handle and store your information, but If you need something easy to set up and simple to operable, I'd go for arrays :
$ventriloStatus = array();
$ventriloStatus['requestObj'] = $stat->Request();
$ventriloStatus['requestMsg'] = "CVentriloStatus->Request() failed. <strong>$stat->m_error</strong><br><br>\n";
Add your data using keys.
Then retrieve the value easily :
echo $ventriloStatus['requestMsg'];
You can even parse your data using a simple loop
foreach($ventriloStatus as $key => $value){
echo $key.' : '.$value.'<br />';

Categories