简体   繁体   中英

Codeigniter function data that will not show undefined variable error

I want build a function in Codeigniter that will not show undefined variable error on view page, if variable is not set than no error or if variable is set then print same value.

I write this following function but this function not working.

    if (!function_exists('_e')) {
     function _e( $data ) {
      if(!isset($data))
        return false;
      else
        echo $data;
      }
    }
 if (!function_exists('_e')) {
     function _e( $data ) {
      if(!isset($data))
        return "";
      else
        return $data;
      }
    }

This should help you out.

try this

if (!function_exists('_e')) {
function _e( $variable ) {

     /*do anything to set $data if you want example below */
     $data = str_replace('WTF', 'WTH' $variable); //<< example 

      if(!isset($data)){
        return false;
      }else{
        return $data;
      }
 }
 }

dont forget to set your variable first. then you can try to echo or print_r for test.

example

<?php
$variable = 'this is WTF';
print_r( _e($variable) ); // << print_r for testing, or use echo _e($data);
?>

notes: if your file is in other php file. dont forget to call file first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM