OOP In PHP (Object Oriented Programming in PHP)

Posted by chaabant on Mar 28, 2008

This is a basic introduction on how to use OOP - Oriented Programming In php

Declaring a Class

  1. class myClass {
  2. // Class contents go here
  3. }

Instantiating an Object

  1. $myClassInst = new myClass();

OOP Class Inheritance

  1. class a {
  2.  
  3. function test()
  4. {
  5. echo "a::test called";
  6. }
  7.  
  8. function func()
  9. {
  10. echo "a::func called";
  11. }
  12. }
  13.  
  14.  
  15. class b extends a {
  16.  
  17. function test()
  18. {
  19. echo "b::test called";
  20. }
  21. }
  22.  
  23. class c extends b {
  24.  
  25. function test()
  26. {
  27. parent::test();
  28. }
  29.  
  30. }
  31.  
  32. class d extends c {
  33.  
  34. function test()
  35. {
  36. b::test();
  37. }
  38.  
  39. }
  40.  
  41. $a = new a();
  42. $b = new b();
  43. $c = new c();
  44. $d = new d();
  45.  
  46. $a->test(); // Outputs "a::test called"
  47. $b->test(); // Outputs "b::test called"
  48. $b->func(); // Outputs "a::func called"
  49. $c->test(); // Outputs "b::test called"
  50. $d->test(); // Outputs "b::test called"

Occurrence of a word in a sentence using php

Posted by chaabant on Jan 9, 2008

Can someone please post a code that will return a number of how many instances of a string are in a string? For example:

“my name is tim tim is a dumb name i hate people named tim”

I want a code to return “3″ (for the 3 instances of tim) for that. Please help!

here is the code :

  1. <?php
  2.  
  3. $i=0; // number of occurrence
  4. $msg = "my name is tim tim is a dumb name i hate people named tim"; // The Sentence
  5. $word = "tim"; // Word to Search
  6.  
  7.  
  8. $txt = explode(" ", $msg); // place the sentence in an array
  9.  
  10.  
  11.  
  12. foreach ($txt as $key=>$val) {
  13.  if($val == $word) {
  14.      $i++ ;
  15.    } // end if
  16.  } // end for
  17.  
  18.  
  19. echo "The Occurrence of $word in the Sentence is : <b> $i </b> ";
  20.  
  21. ?>

Result :

The Occurrence of tim in the Sentence is : 3

Take note that in php there is a difference when using a capital letter , Tim is Different than tim ; A Solution for this would be to use the string strtolower ( string $str ) function .

in our case :

  1. $msg = "my name is tim tim is a dumb name i hate people named tim"; // The Sentence
  2. $msg = strtolower($msg);

Hope this answer your question ;)


Generate Numbers in PHP

Posted by chaabant on Jan 5, 2008

I have no clue about programming and my knowledge only goes as far as excel! …was wondering if someone could lead me in the direction of what program to use to do this:

I basically need 50,000 numbers, each 11 digits long outputted into a text file (.txt)
For example 07850069737

The conditions are:

1st digit must be a zero
2nd digit must be a seven
3rd digit can be a 5 or 7 or 8 or 9
All the other digits are random
Each 11 digit number can only appear once in the 50,000.

So the text file will list the numbers like this (one number per line)

07543098434
07984304577
07948498357
07859488334

I think it’s pretty simple but not sure where to start.

Thanks in advance!

This is a kind of solution , but it’s not 100% it don’t test for uniqueness of the numbers .. you can add them to a database and set the field as unique .

  1. <?php
  2.  
  3.  
  4.  
  5. $myFile = "test.txt"; // The file that we will write to
  6. $fh = fopen($myFile, ‘w’) or die("can’t open file"); // Open the File
  7.  
  8.  
  9. $num1 = "07"; // first 2 numbers
  10.  
  11.  
  12. $var2 = array(5,7,8,9); // 3rd digit
  13.  
  14.  
  15. for($i=0 ; $i < 50000 ; $i++) {
  16.  
  17. srand((float) microtime() * 10000000);
  18.  
  19. // generating the 3rd digit randomnly
  20. $tnum2 = array_rand($var2);
  21. $num2 = $var2[$tnum2] ;
  22.  
  23.  
  24.  
  25. $num3 = rand(10000000,99999999); //generating last 8 digits
  26.  
  27.  
  28. // concatenating all together
  29. $real = $num1.$num2.$num3."\n" ;
  30.  
  31. fwrite($fh, $real);
  32.  
  33. }
  34.  
  35. fclose($fh); // close the file
  36.  
  37. echo "Done !";
  38.  
  39. ?>

Tested this on my local server and got some results :

07952105102
07980381164
07552621459
07786116027
07848509826
07731585388
07886201171
07790208435
07915254211
07758831481
07784220886
07933436584
07752843933
07824007568
07721466979
07914276428
07537883300
07584597167
07541311035
07588024902
07741497802
07943461608
07839547729
07862003784
07884459838
07772586364 …


rss feed to html

Posted by chaabant on Jan 1, 2008

This post is about how to take rss feeds and convert it to HTML for display issues .

Note : This is a basic methods , but the aim of it could be for affiliate marketing , anyone could take this and convert it to an ebay store using his affiliate id .

Since the rss feed is always updated , the site will update it self .

so here is the source code for it :

  1. <?php
  2.  
  3. $url = "http://www.chaaban.info/?feed=rss"; // url feed of my blog , it’s just an wordpress xml file …
  4.  
  5. $xml = new SimpleXMLElement($url, null, true);
  6.  
  7.  
  8. foreach($xml->channel->item as $itm){
  9.  
  10. echo "<a href=\"$itm->link\"> $itm->title </a> ";
  11. echo "<br />";
  12. }
  13.  
  14. ?>

Data Types in PHP

Posted by chaabant on Dec 28, 2007

Data Types in PHP

In php there is different types of data , the most used are :

string // binary data
boolean // true or false
int // Integer

Integers are numbers; Positive and negative numbers can be expressed with it).

Boolean contain two values: false or true .

String Are text , it could be also a collection of binary data (contents of a doc file or image or even mp3 file)

Declaring Variables in PHP

To declare variables in php all what you need to do is place a $ at the beginging of a name that you will give to your variable to refer it .

ex :

  1. $my_number = 13 ; // declaring variable called my_number
  2.  
  3. $my_name = "Chaaban"; // declaring a variable called "my_name" that contain  : Chaaban ;
  4.  
  5. $whatever = true ;     // declaring a variable called "whatever" that  contain a boolean value of true .

php is different than other languages , you can declare a variable that contain a number and later replace it with a string or boolean .

ex :

  1. $x = 10 ;
  2.  
  3. // random codes
  4. // …
  5. // random codes
  6.  
  7. $x = "HELLO WORLD";
  8.  
  9. echo $x ; // will display : HELLO WORLD instead of 10

Invalid names when Declaring a Variable

Variables must be named using only letters (a-z, A-Z), numbers and the underscore character; Their namesmust start with either a letter or an underscore, and are one of only two identifier types in PHP that are case sensitive

  1. $txt   = ’valid’ ;  // valid
  2. $_txt  = ’valid’;   // Valid variable name
  3. $10txt = ’invalid’; // Invalid varianle name since it starts with a number