Php print all variables

Printing php variables in html

Everything I have tried gives me an Parse error: syntax error, unexpected T_VARIABLE error however. Example: or concatenate vars with dots: Sometimes the error depends on single or double quotes.

Printing php variables in html

I’ve been tying myself in knots a bit trying to figure out how to print a couple of PHP variables into some HTML. I just can’t seem to get the single or double quotes correct.

I am pulling images from the 500px api and I want to render them into a bootstrap carousel. The variables I want to use are $photo->name (which I have put in between the heading tags below and $photo->url which I want to put in instead of the placehold.it url. Everything I have tried gives me an Parse error: syntax error, unexpected T_VARIABLE error however.

photos as $photo) < if ($i==0) < print '
$photo->name
'; > else < print "hello 2"; >$i++; > ?>

There are several ways, using «echo», including:

$world = "world"; #If you wrap your statement in double-quotes, then: echo "Hello $world, nice to meet you."; #If you wrap your echo statement in single-quotes, then: echo 'Hello '.$world.', nice to meet you.'; 

So, you might consider changing your code to:

photos as $photo) < if ($i==0) < print '
'.$photo->name.'
'; > else < print "hello 2"; >$i++; >
 photos as $photo) < if ($i==0) < print '
'.$photo->name.'
'; > else < print "hello 2"; >$i++; > ?>
. "background-image:url(\'http://placehold.it/1900x1080&text=Slide One\')". 

Use echo and paste html5 code between double quotes. Example:

echo " or concatenate vars with dots:
 echo " Sometimes the error depends on single or double quotes.

Printing php variables in html, Printing php variables in html. Ask Question Asked 8 years, 6 months ago. Modified 8 years, 6 months ago. Viewed 2k times 0 I’ve been tying myself in knots a bit trying to figure out how to print a couple of PHP variables into some HTML. I just can’t seem to get the Code samplephotos as $photo)

How to print name of variable in PHP?

I’m writing a simple script for my webby that would send me an e-mail in case error 500 happens. I want to dump all possible variables, sessions, POSTs or whatever, present at time when error happened, so I can analyze the problem as precise as I can.

Here’s the code I have for now:

function variable_name( &$var ) < $var_name = array_search( $var, $GLOBALS ); return "= \"\""; > $bar = "whatever"; echo variable_name( $bar ); // bar = "whatever" 

It’s checking just $GLOBALS, but I need something that would check and print also $_POST, $_SESSION, class fields etc. I googled a bit and found just complicated functions that seem like an overkill for such easy task. Is there anything simple for this purpose or should I simply write a function for each of variable types?

This function returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.

this includes $GLOBALS (since PHP 5.0.0), $_POST , $_SESSION , etc.

Читайте также:  jQuery Multi Select Dropdown with Checkboxes

Have you tried the get_defined_vars function ? http://fr2.php.net/manual/fr/function.get-defined-vars.php

Ondrej, try the print_r function. Look it up in the manual, it’s really helpful at times. Notice it also takes an optional second parameter which is useful when you want to log the output to a file or db instead of just printing it to screen.

how about get_defined_vars ? check out get_defined_functions and get_defined_constants as well

Php — Print all variables available in a Smarty template, Use From the manual: dumps the debug console to the page. This works regardless of the debug settings in the php script. Since this gets executed at runtime, this is only able to show the assigned variables; not the templates that are in use. However, you can see all the currently available …

What is the PHP shorthand for: print var if var exist

We’ve all encountered it before, needing to print a variable in an input field but not knowing for sure whether the var is set, like this. Basically this is to avoid an e_warning.

How can I write this shorter? I’m okay introducing a new function like this:

But I don’t succeed in writing the printvar() function.

For PHP >= 5.x:

My recommendation would be to create a issetor function:

function issetor(&$var, $default = false)

This takes a variable as argument and returns it, if it exists, or a default value, if it doesn’t. Now you can do:

But also use it in other cases:

$user = issetor($_GET['user'], 'guest'); 

For PHP >= 7.0:

As of PHP 7 you can use the null-coalesce operator:

Источник

Printing all defined variables and values

This will output information about the class name, parameters and methods including encapsulation information and the number of parameters, names of parameters for each method, method location and lines of code where it exists. Question: This is just giving me the arrays for defined variables but not priniting out any variables.

Printing all defined variables and values

’; print_r(get_defined_vars()); echo ‘

’; ?>

This is just giving me the arrays for defined variables but not priniting out any variables. How can I print values too?

Also what function can I use to output all the defined variables in this format:

Variable name | Variable Type [int, array, string, bool] | Variable defined on line | Variable defined in script | Variable used times | Variable Value

The php documentation should help.

You can make your own debug output, by iterating through the array of returned to display gettype, value and name. However, the where it was defined, how much its been used, you cant do. PHP doesnt work that way

How to properly print variable to PHP error log, print_r will print the array and won’t return a value, so basically you’re using it wrong. The right way is using the second parameter of the function which is boolean that determines if the function will print the output or will return it.

List all PHP variables

Is it possible to dump all global variables in a PHP script? Say this is my code:

Also, is it possible to dump all defined constants in a PHP script.

Use get_defined_vars and/or get_defined_constants

$arr = get_defined_vars(); print_r($arr); 

When debugging trying to find differences using a program such as WinMerge (freeware) to see what differences various arrays and variables have you’ll want to ksort() otherwise you’ll get lots of false negatives. It also helps to visually format using the HTML pre element.

Edit: had to come back to this and realized I had a better answer, $GLOBALS .

$a = print_r(var_dump($GLOBALS),1); echo '
'; echo htmlspecialchars($a); echo '

';

Edit 2: as mpag mentioned print_r() may be susceptible to running out of memory if the software you’re working with uses a lot. Presuming there is no output or it’s clearly truncated and you have access to the php.ini file you can adjust the memory use as so:

ini_set('memory_limit', '1024M'); 

List all PHP variables, Edit 2: as mpag mentioned print_r() may be susceptible to running out of memory if the software you’re working with uses a lot. Presuming there is no output or it’s clearly truncated and you have access to the php.ini file you can adjust the memory use as so: ini_set(‘memory_limit’, ‘1024M’);

How to print all session variables currently set?

Without having to call each session variable by name, is there a way to display the content of all the session variables currently set?

echo '
'; var_dump($_SESSION); echo '

';

Or you can use print_r if you don’t care about types. If you use print_r , you can make the second argument TRUE so it will return instead of echo, useful for.

echo '
' . print_r($_SESSION, TRUE) . '

';

 PHP List All Session Variables"; foreach ($_SESSION as $key=>$val) echo $key." ".$val."
"; ?>

Let’s say that by «active» you mean «hasn’t passed the maximum lifetime» and hasn’t been explicitly destroyed and that you’re using the default session handler.

  • First, the maximum lifetime is defined as a php.ini config and is defined in terms of the last activity on the session. So the «expiry» mechanism would have to read the content of the sessions to determine the application-defined expiry.
  • Second, you’d have to manually read the sessions directory and read the files, whose format I don’t even know they’re in.

If you really need this, you must implement some sort of custom session handler. See session_set_save_handler .

Take also in consideration that you’ll have no feedback if the user just closes the browser or moves away from your site without explciitly logging out. Depending on much inactivity you consider the threshold to deem a session «inactive», the number of false positives you’ll get may be very high.

Array ( [__ci_last_regenerate] => 1490879962 [user_id] => 3 [designation_name] => Admin [region_name] => admin [territory_name] => admin [designation_id] => 2 [region_id] => 1 [territory_id] => 1 [employee_user_id] => mosin11 ) 

Php — How to retrieve all Variables from a Twig Template, After using duncan’s answer for quite some time I finally found the «right» way to dump all twig variables of a template: <% dump %>That’s it. All the variables available in the template will be dumped and in the dump section of the profiler, not in the middle of your html like with < < dump () >>.

How to print all properties of an object

I have an unknown object in php page.

How can I print/echo it, so I can see what properties/values do it have?

What about functions? Is there any way to know what functions an object have?

These are the same things you use for arrays too.

These will show protected and private properties of objects with PHP 5. Static class members will not be shown according to the manual.

If you want to know the member methods you can use get_class_methods():

$class_methods = get_class_methods('myclass'); // or $class_methods = get_class_methods(new myclass()); foreach ($class_methods as $method_name) < echo "$method_name
"; >

As no one has not provided an OO approach yet here is like it would be done.

class Person < public $name = 'Alex Super Tramp'; public $age = 100; private $property = 'property'; >$r = new ReflectionClass(new Person); print_r($r->getProperties()); //Outputs Array ( [0] => ReflectionProperty Object ( [name] => name [class] => Person ) [1] => ReflectionProperty Object ( [name] => age [class] => Person ) [2] => ReflectionProperty Object ( [name] => property [class] => Person ) ) 

The advantage when using reflection is that you can filter by visibility of property, like this:

print_r($r->getProperties(ReflectionProperty::IS_PRIVATE)); 

Since Person::$property is private it’s returned when filtering by IS_PRIVATE:

//Outputs Array ( [0] => ReflectionProperty Object ( [name] => property [class] => Person ) ) 

If you want more info you can use a ReflectionClass:

To get more information use this custom TO($someObject) function:

I wrote this simple function which not only displays the methods of a given object, but also shows its properties, encapsulation and some other useful information like release notes if given.

function TO($object) < //Test Object if(!is_object($object))< throw new Exception("This is not a Object"); return; >if(class_exists(get_class($object), true)) echo "
CLASS NAME = ".get_class($object); $reflection = new ReflectionClass(get_class($object)); echo "
"; echo $reflection->getDocComment(); echo "
"; $metody = $reflection->getMethods(); foreach($metody as $key => $value)< echo "
". $value; > echo "
"; $vars = $reflection->getProperties(); foreach($vars as $key => $value)< echo "
". $value; > echo "

"; >

To show you how it works I will create now some random example class. Lets create class called Person and lets place some release notes just above the class declaration:

 /** * DocNotes - This is description of this class if given else it will display false */ class Person< private $name; private $dob; private $height; private $weight; private static $num; function __construct($dbo, $height, $weight, $name) < $this->dob = $dbo; $this->height = (integer)$height; $this->weight = (integer)$weight; $this->name = $name; self::$num++; > public function eat($var="", $sar="") < echo $var; >public function potrzeba($var ="") < return $var; >> 

Now lets create a instance of a Person and wrap it with our function.

 $Wictor = new Person("27.04.1987", 170, 70, "Wictor"); TO($Wictor); 

This will output information about the class name, parameters and methods including encapsulation information and the number of parameters, names of parameters for each method, method location and lines of code where it exists. See the output below:

CLASS NAME = Person /** * DocNotes - This is description of this class if given else it will display false */ Method [ public method __construct ] < @@ C:\xampp\htdocs\www\kurs_php_zaawansowany\index.php 75 - 82 - Parameters [4] < Parameter #0 [ $dbo ] Parameter #1 [ $height ] Parameter #2 [ $weight ] Parameter #3 [ $name ] >> Method [ public method eat ] < @@ C:\xampp\htdocs\www\kurs_php_zaawansowany\index.php 83 - 85 - Parameters [2] < Parameter #0 [ $var = '' ] Parameter #1 [ $sar = '' ] >> Method [ public method potrzeba ] < @@ C:\xampp\htdocs\www\kurs_php_zaawansowany\index.php 86 - 88 - Parameters [1] < Parameter #0 [ $var = '' ] >> Property [ private $name ] Property [ private $dob ] Property [ private $height ] Property [ private $weight ] Property [ private static $num ] 

Printing a php variable as it is, printing a php variable as it is : with all the special characters. Ask Question Asked 10 years, 5 months ago. Modified 10 years, 5 months ago. Viewed 4k times 0 Ok I need to find out what is contained inside a PHP variable and I have it to do it visually, is there a function to display whatever that’s contained in a …

Источник

Оцените статью