Selamat datang di ForSa! Forum diskusi seputar sains, teknologi dan pendidikan Indonesia.
0 Anggota dan 3 Pengunjung sedang melihat topik ini.
Silahkan kalo yang mau bertanya tentang algoritma di pemrograman?
<?php/** Ohhh... ternyata situs forsa juga pakai PHP **//** Contoh definisi variabel **/$say_yes = "Yes...";/** Contoh definisi constant **/define ('__MY_CONSTANT__', 'this is constant...');/** Contoh statement dengan keyword "if" **/if (defined('__MY_CONSTANT__')){ print __MY__CONSTANT__ . "\n";} else { print "__MY_CONTANT__ constant is undefined...\n";}/** Contoh fungsi print, identik dengan fungsi echo **/print $say_yes; // This will output - Yes.../** * Contoh deklarasi fungsi bebas (non-OO) */function function_name ($param1, $param2, $optional = true){ // Add some codes here...}/** * Contoh deklarasi class - PHP4 style */class My_Class { /** Class field member **/ var $field_example; /** Constructor, PHP4 Style **/ function My_Class ($param) { // Intializations are here... $this->field_example = $param; } /** access function **/ function get_field_ex_value () { return $this->field_example; } }/** * Contoh deklarasi class - PHP5 style */class My_Class_PHP5 { /** Class field member - private **/ private $_priv_field; /** Class filed member - public **/ public $pub_field; /** Constructor, PHP5 Style **/ function __construct ($param1, $param2) { // Intializations are here... $this->_priv_field = $param1; $this->pub_field = $param2; } /** public access function **/ public final function get_priv_field_value () { return $this->__get_priv_field(); } /** public access function **/ public final function get_priv_field_value () { return $this->pub_field; } /** private access function **/ private function __get_priv_field() { return $this->_priv_field; } }/** Defining array variable **/$my_array = array ("Richard M. Stallman", "Rasmus Redlof", "Larry Walls", "Linuz B. Thorvalds");/** Loop with use "foreach" keyword **/foreach ($my_array as $entry){ print "$entry\n";}/** Loop with "for" keyword **/for ($i = 0; $i < count($my_array); $i++){ print $my_array[$i] . "\n";}/** Loop with "do" and "while" keyword **/$count = 0;do { print $my_array[$count] . "\n"; $count++;} while ($count < count($my_array)-1);
char *ptr = "foo";/** * set_string_ptr: * @p: string to be copied into @ptr * * This will safely copies string from @p to @ptr with proper * memory allocation with use native malloc() function. */voidset_string_ptr (const char *p){ ptr = (char*) malloc (strlen (p)); strcpy (ptr, p);}