Log in

View Full Version : [PHP] Explain that function!



Jackboy
06-05-2008, 11:42 AM
Well its very boring at the moment, and i can see a few web programmers are online :o

So i thought (to some peoples displeasure) that it would be fun to make a thread, letting people explain some functions etc.

And of course YES you can go to php.net, but some people find the site of it scary (although its a fit resource).

There are many functions that some people may know which others may have never heard of.

YOU MAY POST DEFINITIONS OFF OTHER SITES, BUT YOU MUST CREDIT THEM.


I will start off nice and easy :)

echo();



<?PHP
echo "Hello world!";
?>
Outputs all parameters.
echo() is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo() (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo(), the parameters must not be enclosed within parentheses.

TAKEN FROM PHP.NET

Florx
06-05-2008, 02:44 PM
What. the. hell. is. the. point. in. this. thread?

Let us all copy pages from PHP.net. WOO.

Agnostic Bear
06-05-2008, 02:58 PM
Well its very boring at the moment, and i can see a few web programmers are online :o

So i thought (to some peoples displeasure) that it would be fun to make a thread, letting people explain some functions etc.

And of course YES you can go to php.net, but some people find the site of it scary (although its a fit resource).

There are many functions that some people may know which others may have never heard of.

YOU MAY POST DEFINITIONS OFF OTHER SITES, BUT YOU MUST CREDIT THEM.


I will start off nice and easy :)

echo();



<?PHP
echo "Hello world!";
?>
Outputs all parameters.
echo() is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo() (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo(), the parameters must not be enclosed within parentheses.

TAKEN FROM PHP.NET

Should be echo 'hello' unless you need literal newlines etc.

Decode
06-05-2008, 03:10 PM
<?php
echo "<a href=\"http://php.net\">Click here for all definitions, then we dont need this thread :)</a>";
?>

Florx
06-05-2008, 03:18 PM
Wahey. Nicely done.

Agnostic Bear
06-05-2008, 03:21 PM
<?php
echo "<a href=\"http://php.net\">Click here for all definitions, then we dont need this thread :)</a>";
?>

negative:

echo( '<a href="http://www.php.net/en/manual">Click</a>' );

() are my personal preference.

MrCraig
06-05-2008, 03:25 PM
Yeah i like brackets too :P

Doing ""; just makes it confusing lol.

Jackboy
06-05-2008, 03:38 PM
Woah sorry, u lot were at skool and i was bored.

I used to use () but i thought it was improper syntax or w/e..

was i wrong?

L?KE
06-05-2008, 04:29 PM
I always thought brackets was proper syntax. They wouldn't be there for nothing.

I swear
echo "Hello";

is out dated compared to

echo("Hello");

Or am I just being an idiot :)

Jackboy
06-05-2008, 04:41 PM
I always thought brackets was proper syntax. They wouldn't be there for nothing.

I swear
echo "Hello";

is out dated compared to

echo("Hello");

Or am I just being an idiot :)

Well i dunno, i just remember seeing "" without the () so i thought "Z0MG"

Excellent
06-05-2008, 04:51 PM
Both are proper syntax I think but I like using brackets. Only problem with me is I am always forgetting to add the $ signs.

Jackboy
06-05-2008, 04:57 PM
Both are proper syntax I think but I like using brackets. Only problem with me is I am always forgetting to add the $ signs.

Lmao how can u forget :p

Its a variable, and variables have $ :P

Protege
06-05-2008, 05:01 PM
I always use brackets tho i get criticized a lot for doing it. Like I use brackets where some people don't eg: "return(true);" or "print("eg");" or "include("eg.php");" etc



Both are proper syntax I think but I like using brackets. Only problem with me is I am always forgetting to add the $ signs.


I usually do that if I left PHP for along time and I've been programming in vb.net loool

Fehm
06-05-2008, 05:01 PM
Lol why this thread. but i might as well join in

(Why am i doing this ///&%#FR$%$# > Click>Echo(')D(&#$NFI)

:) I can code PHP :d (Clappage)

Excellent
06-05-2008, 05:04 PM
Lmao how can u forget :p

Its a variable, and variables have $ :PWell it's pretty simple to forget. Well done!:eusa_clap


Lol why this thread. but i might as well join in

(Why am i doing this ///&%#FR$%$# > Click>Echo(')D(&#$NFI)

:) I can code PHP :d (Clappage)Fail, -1.

Protege
06-05-2008, 05:06 PM
Seeing as people are putting echos I'll do this

<?php
echo('<meta http-equiv="refresh" content="0;url=http://php.net">')
?>

Excellent
06-05-2008, 05:12 PM
Lol why this thread. but i might as well join in

(Why am i doing this ///&%#FR$%$# > Click>Echo(')D(&#$NFI)

:) I can code PHP :d (Clappage)

$knownSinner = mysql_query("SELECT * FROM `idiot` WHERE mg_name = '$fail' ");

Jackboy
06-05-2008, 05:27 PM
Seeing as people are putting echos I'll do this

<?php
echo('<meta http-equiv="refresh" content="0;url=http://php.net">')
?>



:O

So if u use (' ') ;

Then you can do "" inside??!?!

I never knew that :)






<?PHP
$knownSinner = mysql_query("SELECT * FROM `idiotlist` WHERE `username` = 'knownSinner'");
$Howmany = mysql_num_rows($knownSinner);
if ($Howmany == '0'){
print "You are only lying to yourself...";
}else{
print "Thats what i thought.";
}
?>

Protege
06-05-2008, 05:47 PM
Correct and you can do it the opposite way round. eg:
<?php echo("<font face='Tahoma'>") ?> ...or you can escape the quoations by adding a blackslash.
eg

<?php echo("<font face=\"Tahoma\">") ?>Its good if your doing this too


<?php echo('It\'s great') ?> your bit of mysql could be better though.



<?PHP
$knownSinner = mysql_query("SELECT * FROM `idiotlist` WHERE `username` = 'knownSinner'");
if(mysql_num_rows($knownSinner) == 0) {
print "You are only lying to yourself...";
} elseif(mysql_num_rows($knownSinner) > 0) {
print "Thats what i thought.";
}
?>
or



<?PHP
$knownSinner = mysql_query("SELECT * FROM `idiotlist` WHERE `username` = 'knownSinner'");
$n = mysql_num_rows($knownSinner);
if($n == 0) {
print "You are only lying to yourself...";
} elseif($n > 0) {
print "Thats what i thought.";
}
?>

Decode
06-05-2008, 06:08 PM
Lol why this thread. but i might as well join in

(Why am i doing this ///&%#FR$%$# > Click>Echo(')D(&#$NFI)

:) I can code PHP :d (Clappage)


Your doing it wrong

Agnostic Bear
06-05-2008, 06:11 PM
Hey I'll even put in a custom function!

$this->load_class( 'DB', 'Kernel' );

Decode
06-05-2008, 06:13 PM
Hey I'll even put in a custom function!

$this->load_class( 'DB', 'Kernel' );
10/10 lol

Jackboy
06-05-2008, 07:08 PM
<?PHP
$knownSinner = mysql_query("SELECT * FROM `idiotlist` WHERE `username` = 'knownSinner'");
if(mysql_num_rows($knownSinner) == 0) {
print "You are only lying to yourself...";
} elseif(mysql_num_rows($knownSinner) > 0) {
print "Thats what i thought.";
}
?>
or



<?PHP
$knownSinner = mysql_query("SELECT * FROM `idiotlist` WHERE `username` = 'knownSinner'");
$n = mysql_num_rows($knownSinner);
if($n == 0) {
print "You are only lying to yourself...";
} elseif($n > 0) {
print "Thats what i thought.";
}
?>


Wait.. So your saying my mysql could be better?

Im very confused..

Doing if(mysql_num_rows($knownSinner)){ is a longer way of counting the rows...

the second one you have done almost exactly the same except added if($n >0).

There was no need for an elseif as else works just fine.

Protege
06-05-2008, 07:27 PM
whats the point though in using another if statement when you can use the one your alread y using?

CJ-real
06-05-2008, 07:30 PM
I don't see the point in brackets either... unless you are doing functions as in, or headers etc....

function whatever($echo)


ohhhh http://www.php.net/quickref.php

--ss--
06-05-2008, 07:30 PM
<?php
echo("I actually think it's much neater and clearer by having it with brackets surrounding the speech marks but that's just my personal opinion.");
?>

Protege
06-05-2008, 07:36 PM
I agree to that :)

Jackboy
06-05-2008, 07:56 PM
whats the point though in using another if statement when you can use the one your alread y using?

Your the one that did lol

I had else{ and you put elseif{
...

Protege
06-05-2008, 08:18 PM
if($x == 0) {
// error
} elseif($x > 0) {
//succeed
} else {
// error
}

apposed to

if($x == 0) {
// fail
} else {
if($x > 0) {
// succeed
} else {
// fail
}
}

Agnostic Bear
06-05-2008, 08:37 PM
if($x == 0) {
// error
} elseif($x > 0) {
//succeed
} else {
// error
}

apposed to

if($x == 0) {
// fail
} else {
if($x > 0) {
// succeed
} else {
// fail
}
}

Or you could just do
if( $x >= 1 )
{
//success
}
else
{
//fail
}

Protege
06-05-2008, 08:51 PM
Or that.

QuickScriptz
06-05-2008, 09:37 PM
Well, let's all just put down the one guy who's trying to make the Forum a little more interesting.... who cares if you reference the PHP website as a reference? A lot of the time the site can confuse instead of help so I think this thread is a great idea. Anyways...

Today I'm going to talk about the rand() function.


<?php
// rand ( lowestDigit, highestDigit )
echo rand(1,5); // Outputs any of these: 1 2 3 4 5
?>

What this function does is generate a random integer between lowestDigit and highestDigit (note that it includes the lowestDigit and highestDigit in it's number range - basically it can return lowestDigit, highestDigit, and everything in between).

I find this function very helpful when you're trying to generate random CAPTCHAs, License Keys/Key Codes, and anything else that requires random anything.

Hope this helps someone out :)

Jackboy
07-05-2008, 04:29 AM
if($x == 0) {
// error
} elseif($x > 0) {
//succeed
} else {
// error
}

apposed to

if($x == 0) {
// fail
} else {
if($x > 0) {
// succeed
} else {
// fail
}
}

What ar you on about?

I did this:





<?PHP
$knownSinner = mysql_query("SELECT * FROM `idiotlist` WHERE `username` = 'knownSinner'");
$Howmany = mysql_num_rows($knownSinner);
if ($Howmany == '0'){
print "You are only lying to yourself...";
}else{
print "Thats what i thought.";
}
?>

Protege
07-05-2008, 06:05 AM
So what happens if mysql_num_rows returns an error

Jackboy
07-05-2008, 06:25 AM
So what happens if mysql_num_rows returns an error

Why would it?

$knownSinner is a valid resource.

Protege
07-05-2008, 06:51 AM
I meant if it did, "what happens if" it was to error?

Decode
07-05-2008, 07:03 AM
Or you could just do
if( $x >= 1 )
{
//success
}
else
{
//fail
}

or

if( $x >= 1 )
{
echo ("You failed");
}
else
{
echo ("You failed again");
}

Blinger1
07-05-2008, 08:39 AM
you fail.

Baving
07-05-2008, 09:32 AM
I meant if it did, "what happens if" it was to error?

It would return the same error message doing it either as:


if(mysql_num_rows($result)==0) { }

or


$result = mysql_num_rows($query);
if($result==0) { }

mysql_num_rows only converts the query result from mysql_query, it doesn't contact the database again.

Personally I prefer to use:


if(mysql_num_rows($result)==0) { }

It is just the same as putting it into a class and executing it:


if($db->num_rows($query)==0) { }

However the proper way in which to return the amount of rows is by doing it in the SQL itself:

select count(id) as total from tablename where 1=1

Protege
07-05-2008, 10:52 AM
Yea baving im not talkin about that, im talking about his method of doing it.

Hypertext
07-05-2008, 12:10 PM
Rand is how I judge when to use cron, I use
<?php
if((rand(0, 200)) == 1) {
// run cron
}
?>
Of course, once I launch Flyxa I will set the 200 higher, and as it gets more popular etc.

Agnostic Bear
07-05-2008, 12:39 PM
It would return the same error message doing it either as:


if(mysql_num_rows($result)==0) { }or


$result = mysql_num_rows($query);
if($result==0) { } mysql_num_rows only converts the query result from mysql_query, it doesn't contact the database again.

Personally I prefer to use:


if(mysql_num_rows($result)==0) { }It is just the same as putting it into a class and executing it:


if($db->num_rows($query)==0) { }However the proper way in which to return the amount of rows is by doing it in the SQL itself:

select count(id) as total from tablename where 1=1

Both are proper ways. And both end up with the same result, it's just select count() is faster.

also @ jack, this is faster than yours if you insist on using mysql_num_rows:

<?php
$knownSinner = mysql_query("SELECT * FROM `idiotlist` WHERE `username` = 'knownSinner'");
$Howmany = mysql_num_rows($knownSinner);
if ($Howmany === 0)
{
echo( 'You are only lying to yourself...' );
}
else
{
echo( 'That\'s what i thought.' );
}
?>

Jackboy
07-05-2008, 03:48 PM
It would return the same error message doing it either as:


if(mysql_num_rows($result)==0) { }or


$result = mysql_num_rows($query);
if($result==0) { } mysql_num_rows only converts the query result from mysql_query, it doesn't contact the database again.

Personally I prefer to use:


if(mysql_num_rows($result)==0) { }It is just the same as putting it into a class and executing it:


if($db->num_rows($query)==0) { }However the proper way in which to return the amount of rows is by doing it in the SQL itself:

select count(id) as total from tablename where 1=1

Ok thanks





Both are proper ways. And both end up with the same result, it's just select count() is faster.

also @ jack, this is faster than yours if you insist on using mysql_num_rows:

<?php
$knownSinner = mysql_query("SELECT * FROM `idiotlist` WHERE `username` = 'knownSinner'");
$Howmany = mysql_num_rows($knownSinner);
if ($Howmany === 0)
{
echo( 'You are only lying to yourself...' );
}
else
{
echo( 'That\'s what i thought.' );
}
?>

Sorry, i got used to doing mysql_num_rows, please explain why thats faster :)

Agnostic Bear
07-05-2008, 03:49 PM
Ok thanks






Sorry, i got used to doing mysql_num_rows, please explain why thats faster :)

Doing a query then counting the rows is slower than just doing a query to return the count of the rows.

=== is faster than == when it's the right situation to use it, and ' is faster than ", use " when you need interpreted stuff (i.e: \n \t) else don't use it atall.

Jackboy
07-05-2008, 04:17 PM
Doing a query then counting the rows is slower than just doing a query to return the count of the rows.

=== is faster than == when it's the right situation to use it, and ' is faster than ", use " when you need interpreted stuff (i.e: \n \t) else don't use it atall.

Ahh i never knew that, this thread is helping me learn things, cheers.

Want to hide these adverts? Register an account for free!