PHP Cheat Sheet Print

  • 3


/************************************************************************************************************
 * Common Syntax
 ************************************************************************************************************/

  // Returns true if UNDEFINED, or value is blank, zero(numeric), false(bool)
  empty( $variable );

  // Returns true if DEFINED
  isset( $variable );

  // Arrays
  $report['arrayFromString']    = explode(',', (string)'a,2,b,2,c,3' );
  $report['array']              = array(0,1,2,3,4);
  $report['joinString']         = implode(', ', $report['array']);

  // Associative Arrays
  $report['assocArray']         = array('key'=>'val', 'key2'=>'val2');
  $report['assocKeys']          = array_keys( $report['assocArray'] );

  // Objects
  $report['object']             = (object)array(
                                    'key'=>'val',
                                    'key2'=>'val2'
                                    );
  $report['objectVars']         = get_object_vars( $report['object'] );

  // Standard Class
  $report['stdClass']           = new stdClass();
  $report['stdClass']->key1 = 'val1';
  $report['stdClass']->{'key2'} = 'val2';
  $report['stdClassName']       = get_class( $report['stdClass'] );
  $report['stdClassVars']       = get_object_vars( $report['stdClass'] );
  $report['stdClassKeys']       = array_keys(get_object_vars( $report['stdClass'] ));

  // Custom Class Definition
  class myCustomClass {

    private $data       = array('Hello');
    public static $var  = 'value';

    public function __construct(){
    }

    public function __destruct(){
    }

    public function myFunc(){
      print_r( $this->data );
    }

    public static function myStatFunc(){
      echo self::$var;
    }

  }

  // Custom Class
  $report['class']              = new myCustomClass();
  $report['class']->myFunc();
  $report['className']          = get_class( $report['class'] );
  $report['classVars']          = get_object_vars( $report['class'] );
  $report['classKeys']          = array_keys(get_object_vars( $report['class'] ));

  myCustomClass::myStatFunc();
  $report['inspectClassVars']   = get_class_vars( myCustomClass );
  $report['inspectClassKeys']   = array_keys(get_class_vars( myCustomClass ));

  // Report Building
  $report['html']               = array(
                                    '<table>'
                                    );
  $report['html'][] = '<tr>';
  $report['html'][] = '<td> Hello </td>';
  $report['html'][] = '</tr>';
  $report['html'][] = '</table>';
  $report['finalHtml'] = implode("\n", $report['html']);

  // Echo Report
  // echo '<pre>' . print_r( $report, true ) . '</pre>';



Was this answer helpful?

« Back