Tag Archives: php

Good Practices in PHP

Here are a few tips for those of you who’d like to write good code in PHP: Set up a Development Server Set up a development server or sandbox. Don’t write, test, or develop code on a production server. If you are a *nix user, you probably already have a LAMP environment on your own computer. If you are a Windows user, try XAMPP or WAMPSERVER (personal favorite). If you are on mac, try MAMP. As you develop your PHP, you will need to be learning about the environment which it runs in, and the settings that will affect your code and/or application. This is a journey, so don’t expect to master it all at once. You will get into things like virtual hosts, .htaccess files, rewrite rules, and file permissions. Turn on Error Reporting At least, on your development server. On your production site, turn off displaying errors, but have them sent to a file. Check this file periodically as it will show you what unexpected things are happening in your code. Do’s and Don’t’s Don’t use short codes. They take longer to type, but PHP will be moving away from them in future versions. Also, the <? syntax can be mistaken for XML. Examples of short codes include <? instead of <?php, and also the syntax <?= $var ?> which is the easy way of writing <?php echo $var; ?> Do use comments and document your code. Don’t use a closing ?> at the end of your PHP files. … Continue reading

Posted in Tech Opinion, Tech Tips, Web Development | Tagged , | Leave a comment

Complete list of built-in PHP functions

Phar PharData PharException PharFileInfo abs acos acosh addcslashes addslashes aggregate aggregate_info aggregate_methods aggregate_methods_by_list aggregate_methods_by_regexp aggregate_properties aggregate_properties_by_list aggregate_properties_by_regexp aggregation_info apache_child_terminate apache_get_modules apache_get_version apache_getenv apache_lookup_uri apache_note apache_request_headers apache_reset_timeout apache_response_headers apache_setenv apc_add apc_cache_info apc_clear_cache apc_compile_file apc_define_constants apc_delete apc_fetch apc_load_constants apc_sma_info apc_store apd_breakpoint apd_callstack apd_clunk apd_continue apd_croak apd_dump_function_table apd_dump_persistent_resources apd_dump_regular_resources apd_echo apd_get_active_symbols apd_set_pprof_trace apd_set_session apd_set_session_trace apd_set_session_trace_socket array array_change_key_case array_chunk array_combine array_count_values array_diff array_diff_assoc array_diff_key array_diff_uassoc array_diff_ukey array_fill array_fill_keys array_filter array_flip array_intersect array_intersect_assoc array_intersect_key array_intersect_uassoc array_intersect_ukey array_key_exists array_keys array_map array_merge array_merge_recursive array_multisort array_pad array_pop array_product array_push array_rand array_reduce array_reverse array_search array_shift array_slice array_splice array_sum array_udiff array_udiff_assoc array_udiff_uassoc array_uintersect array_uintersect_assoc array_uintersect_uassoc array_unique array_unshift array_values array_walk array_walk_recursive arrayaccess arrayiterator arrayobject arsort ascii2ebcdic asin asinh asort assert assert_options atan atan2 atanh audioproperties badfunctioncallexception badmethodcallexception base64_decode base64_encode base_convert basename bbcode_add_element bbcode_add_smiley bbcode_create bbcode_destroy bbcode_parse bbcode_set_arg_parser bbcode_set_flags bcadd bccomp bcdiv bcmod bcmul bcompiler_load bcompiler_load_exe bcompiler_parse_class bcompiler_read bcompiler_write_class bcompiler_write_constant bcompiler_write_exe_footer bcompiler_write_file bcompiler_write_footer bcompiler_write_function bcompiler_write_functions_from_file bcompiler_write_header bcompiler_write_included_filename bcpow bcpowmod bcscale bcsqrt bcsub bin2hex bind_textdomain_codeset bindec bindtextdomain bumpValue bzclose bzcompress bzdecompress bzerrno bzerror bzerrstr bzflush bzopen bzread bzwrite cachingiterator cal_days_in_month cal_from_jd cal_info cal_to_jd calcul_hmac calculhmac call_user_func call_user_func_array call_user_method call_user_method_array ceil chdir checkdate checkdnsrr chgrp chmod chop chown chr chroot chunk_split class_exists class_implements class_parents classkit_import classkit_method_add classkit_method_copy classkit_method_redefine classkit_method_remove classkit_method_rename clearstatcache closedir closelog collator com com_addref com_create_guid com_event_sink com_get com_get_active_object com_invoke com_isenum com_load com_load_typelib com_message_pump com_print_typeinfo com_propget com_propput com_propset com_release com_set compact connection_aborted connection_status connection_timeout constant construct convert_cyr_string convert_uudecode convert_uuencode copy cos cosh count count_chars countable counter_bump counter_bump_value counter_create counter_get counter_get_meta counter_get_named counter_get_value counter_reset counter_reset_value crack_check crack_closedict crack_getlastmessage crack_opendict crc32 create_function crypt ctype_alnum ctype_alpha … Continue reading

Posted in Web Development | Tagged | 1 Comment

Using json_encode() and json_decode() in PHP4

I use json_encode() a lot for AJAX calls. Teamed with jQuery’s $.getJSON(), it’s too convenient not to use. Unfortunately, json_encode() doesn’t come standard until PHP 5.2. To add insult to injury, many current *nix distros don’t include PHP 5.2 in their official repositories yet. So, if you’re using PHP4 download JSON’s json_encode and json_decode for PHP4 (which automatically degrade for PHP5, thus not breaking your app during an upgrade) here: http://mike.teczno.com/JSON/JSON.phps json_encode() example 1 <?php include("JSON.php"); $a = json_encode( array( ‘a’=>1, ‘2’=>2, ‘c’=>’I <3 JSON’ ) ); echo $a; // Outputs: {"a":1,"b":2,"c":"I <3 JSON"} $b = json_decode( $a ); echo "$b->a, $b->b, $b->c"; // Outputs: 1, 2, I <3 JSON json_encode() / json_decode() example 2 In Javascript, consuming input in JSON format is as easy as: eval(“var decoded_data = ” + encoded_data); With JSON-PHP, it can be almost as easy on the server-side, too: // create a new instance of Services_JSON require_once(‘JSON.php’); $json = new Services_JSON(); // convert a complex value to JSON notation $value = array(1, 2, ‘foo’); $output = $json->encode($value); print($output); // accept incoming POST data $input = $GLOBALS[‘HTTP_RAW_POST_DATA’]; $value = $json->decode($input); Using json_encode() and json_decode() in PHP4 with Arrays You can use json_encode() with arrays or multi-dimensional arrays. When you are ready to output it as json, just call echo json_encode($yourArray); You can also use this method with jQuery’s $.ajax call, and specify the data type as JSON, although jQuery will automatically try and detect the data type, so that may not even be necessary. source: mike.teczno

Posted in Web Development | Tagged , , | 10 Comments