Question:
What is the purpose of the parenthesis in PHP?
Ed
2011-08-30 15:14:29 UTC
In PHP what do parenthesis ( ) mean?

eg. mysql_connect("localhost", "db_username", "db_password") or die(mysql_error() );

And why does (mysql_error() ) have an open/closed parenthesis after the word error?
why not just (mysql_error);

Im interested to learn about this part of php syntax.
Four answers:
RC
2011-08-30 15:35:52 UTC
mysql_error() is a PHP function, therefore it requires the parenthesis.



The parenthesis contain the parameters of a function, regardless of whether the function uses any parameters or not. A parameter is passing a value to the function...



Example:



register($username, $password, $email);



We're calling a function called register that is defined somewhere in our code. We're passing three variables to it ($username, $password and $email) each of which containing what their names describe.



Let's imagine that there's a registration page which allows users to register their accounts, this page contains a form which allows the user to type in their desired username, password and email address. The register adds the inputted username, password and email address to the database.



For the sake of this, let's imagine that the inputted username, password and email are in their respective variables. The register function would not know what the values of these were if they were not passed to the function, so you must pass these values to the function through the parameters.
?
2011-08-30 23:13:43 UTC
They can mean several things, depending on where they are found.



Most commonly, they are found when you want to pass information ("Parameters") into something else ("Functions", "Classes", etc).



Sometimes, functions or classes don't actually need anything passed into them. But the computer is too stupid to know this, so it will need the ()'s anyway. If you forget the ()'s, the computer might misinterpret what you wanted, or just plain fail.





Think of it like this, if you give the computer an order, it needs you to tell it the details, and the details are always in brackets. If there aren't any details - it needs to know that, too!



read_a_book(15); // Makes it read starting on page 15

read_a_book(); // Also makes it read, but the computer will assume it starts on page 1



()'s can also be used for math.

1 + 2 * 3 = 5 // Bedmas will make this try multiplying first

(1 + 2) * 3 = 6 // Brackets will make this add first, since bedmas starts with brackets!





And they can also be used to clear up confusion if it doesn't know how to do what you want:

"I have ".2+5."apples"; // This won't work. it might think you are adding "I have 2" and "5 apples".

"I have ".(2+5)."apples" // Works fine.



Brackets can also be used for several other things, like typecasting. But 99% of your programming will be these scenarios.





In the mysql_error example:

()'s will let the computer know when a function is a function, and not something else. For example, you can create something called a "definition". A definition is just a word with a value - like a variable. Only definitions cannot be changed, and do not require the $.



so I could go...



define("mysql_error", "HI!");

echo(mysql_error()); // Might output "SQL ERROR: You have an error in your..."

echo(mysql_error); // This would output "HI!", because I made that definition.



The ()'s let the computer know that we wanted to call a function, and not anything else. Even though there were no parameters, when you give a computer an order - it expects to know if there are any details - or it will assume you meant something else.
peteams
2011-08-30 23:23:11 UTC
The parenthesis have several meanings, depending on context.



Sometimes they just group operations together like they do in ordinary mathematics, so 3*(2+1) is 9 with the parathesis, but it would be 7 if they were absent.



Things like mysql_error are functions. mysql_error is a value, a reference to the code that implements getting the error code. Putting parethesis after the function name causes the function to be executed: mysql_error has the value of the function, mysql_error() has the value returned by executing the function.
yzfdude1
2011-08-30 22:28:14 UTC
all functions require parenthesis as part of the compiler's ability to recognize them from other elements. The fact that you can call functions without parameters is why you have empty arguments.



string mysql_error ([ resource $link_identifier ] ) - The mysql_error returns a string, and can take a link identifier as an argument if passed.



$link_identifier - The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If no connection is found or established, an E_WARNING level error is generated.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...