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
-
class myClass {
-
// Class contents go here
-
}
Instantiating an Object
-
$myClassInst = new myClass();
OOP Class Inheritance
-
class a {
-
-
function test()
-
{
-
echo "a::test called";
-
}
-
-
function func()
-
{
-
echo "a::func called";
-
}
-
}
-
-
-
class b extends a {
-
-
function test()
-
{
-
echo "b::test called";
-
}
-
}
-
-
class c extends b {
-
-
function test()
-
{
-
parent::test();
-
}
-
-
}
-
-
class d extends c {
-
-
function test()
-
{
-
b::test();
-
}
-
-
}
-
-
$a = new a();
-
$b = new b();
-
$c = new c();
-
$d = new d();
-
-
$a->test(); // Outputs "a::test called"
-
$b->test(); // Outputs "b::test called"
-
$b->func(); // Outputs "a::func called"
-
$c->test(); // Outputs "b::test called"
-
$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 :
-
<?php
-
-
$i=0; // number of occurrence
-
$msg = "my name is tim tim is a dumb name i hate people named tim"; // The Sentence
-
$word = "tim"; // Word to Search
-
-
-
-
-
-
foreach ($txt as $key=>$val) {
-
if($val == $word) {
-
$i++ ;
-
} // end if
-
} // end for
-
-
-
echo "The Occurrence of $word in the Sentence is : <b> $i </b> ";
-
-
?>
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 :
-
$msg = "my name is tim tim is a dumb name i hate people named tim"; // The Sentence
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 07850069737The 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 .
-
<?php
-
-
-
-
$myFile = "test.txt"; // The file that we will write to
-
-
-
$num1 = "07"; // first 2 numbers
-
-
-
-
-
for($i=0 ; $i < 50000 ; $i++) {
-
-
-
// generating the 3rd digit randomnly
-
$num2 = $var2[$tnum2] ;
-
-
-
-
-
-
// concatenating all together
-
$real = $num1.$num2.$num3."\n" ;
-
-
-
}
-
-
-
echo "Done !";
-
-
?>
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 :
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 :
-
$my_number = 13 ; // declaring variable called my_number
-
-
$my_name = "Chaaban"; // declaring a variable called "my_name" that contain : Chaaban ;
-
-
$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 :
-
$x = 10 ;
-
-
// random codes
-
// …
-
// random codes
-
-
$x = "HELLO WORLD";
-
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
-
$txt = ’valid’ ; // valid
-
$_txt = ’valid’; // Valid variable name
-
$10txt = ’invalid’; // Invalid varianle name since it starts with a number