9

What is the difference between hide() and unset()? I want to hide some elements, I found hide() and unset(), but I don't know which one I should use.

1 Answer 1

19

hide() and unset() are not same. hide() is a Drupal function and unset() is a raw php function.

unset() simply destroys a variable. For an example,

<?php
$name = 'Drupal';
unset($name);
print $name; // this will result an error as $name is no longer set. `isset()` will return FALSE. 

hide() does NOT destroy the variable. It's useful to mark some render-able array is already rendered so it will not be rendered again.

it simply adds/replaces the given argument's #printed to TRUE so drupal_render or render() won't execute the theme functions again - instead, it will use cached version if available.

A sample use case of hide() would be to hide comment form in a node template and show it somewhere else in the template. You will also find it useful when altering pages or blocks. Do NOT unset() a render array if you want to use it somewhere else.

If you want to hide some specific form element from the user but still want to keep the form structure, set #access of the element to FALSE.

hide()

drupal_render()

unset()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.