PHP - How Does This Function Work? A Basic Php Question
Hi, Im learning php, but Im a bit confused on this basic problem.
I have the following code: <?php include "tax.php"; $price = 95; echo "Price before tax: $price <br>"; echo "Price after tax: "; echo add_tax ($price); ?> Which uses the function below <?php function add_tax ($amount) { $total = $amount * 1.09; return $total; } ?> When I execute the code I can see that it does work, but I do not understand why this part works! echo add_tax ($price); There is no multiplier symbol here, so is the function performed on the ($price) variable simply because of where it is located on this line of code? Thanks Similar TutorialsHi there, I am starting to learn OO PHP programming. After running math.php, it gives an output 'Use only numbers'. Why? I don't understand this. math.php <?php require('MathClass.php'); $c = new MathClass(); echo $c->add(5,10,15,20); ?> MathClass.php <?php class MathClass{ function add(){ $args = func_num_args(); $sum = 0; $i = 0; for ($i ; $i < $args ; $i++){ is_int(func_get_args($i)) ? $sum += func_get_args($i) : die('Use only numbers'); } return $sum; } } ?> Quote from: http://www.killerphp.com/tutorials/object-oriented-php/php-objects-page-1.php getter and setter names should match the property names. This way, when other PHP programmers want to use your objects, they will know that if you have a method/function called 'set_name()', there will be a property/variable called 'name'. Is this an accurate statement? Even if there's like 20+ properties? That would be 40+ methods and that's not including doing anything with those properties but setting/getting. That sounds...I don't know...against the grain. I would like to keep all my classes in a folder placed within the htdocs folder where my php script runs. Let's say the classes folder is com\jimslounge\classes. From my script can I instantiate a class like this $myClass = new com\jimslounge\classes\Jimsclass(); Does it work something like that? I would hate to have to use include to access classes. Does it matter where you start your session at? Does it have to be before the <!doctype> tag or can it go anywhere in the page? Hi, can you help me please? I have some code written about 5 yrs ago that is for example like this echo"<div class=\"select\">"; echo"<br /><br />\n"; echo"</div>\n"; echo"</div>\n"; echo "<p><b>$address</b></p>\n"; Does it make any difference at all if I amended it to echo"<div class=\"select\"><br /><br /></div></div><p><b>$address</b></p>"; ie coding errors or increase load time? also when should I use \n"; or "; Please forgive my ignorance but I am a self taught php writer and have managed to teach myself but just need a little help. Cheers Hello, Im from the Netherlands and i have a little question about php. I have a form in php where administrators can make new users. They can fill in a name, password, age and can select a role. The role is a dropdown menu where they can select athlete, researcher and trainer. My problem is that when they select a role, i want that selected role transfert to a php variable. I want to use the variable in a if statment, when sporter selected a new dropdown appears with options like tennis, hockey.... I have seen a lot on google about this with jquery and stuff, but i cant find a good example. Can u help me? Thanks. is it okay to name a column "date" in a table ??? just want to know whether this could mess up everything thanks in advance Hello all. I'm learning Object oriented php and found this simple piece of code online but i don't understand why the variable $con must be returned: Code: [Select] class Connect { public static function con() { $con=mysql_connect("localhost","root","");// Connects to DB??? mysql_query("SET NAMES 'utf8'"); // Sets charset to UTF8 mysql_select_db("blog"); // Selects the DB return $con; // Don't know why $con must be returned, but it wont work without this. } } $con is later used in the script like this from another class to run a Query: Code: [Select] mysql_query($somequery,Connect::con()); Thanks for your help. I'm guessing this is more of a hosting/mail question than specifically php, but here goes... I have this very basic code to send a test email: <?php $to = 'me@mydomain.com'; $subject = 'Test email'; $message = "Hello World!"; $mail_sent = mail( $to, $subject, $message); echo $mail_sent ? "Mail sent" : "Mail failed"; ?> and the following in my php5.ini: Quote [mail function] SMTP = relay-hosting.secureserver.net sendmail_from = admin@malvernaquarium.com When the mail is received, it's showing as coming from "auserid@p3nlh263.shr.prod.phx3.secureserver.net". How can I get this to show as something more reasonable and related to my domain? Ideally something more like admin@malvernaquarium.com. Thanks in advance! Greg Hello!
I'm newbie in PHP(started 3 days ago), and i have two questions about the same topic, i hope you can answer me.
1- Is there methods in php for interaction with webpages as javascript, as they simulate a user interacting with these pages? For example, the method click () or the getElementsByClassName () for you to find something specific on the page. There are methods that do this in php?
2- To interact with a website using a sript (to click on links or save the url of pictures in an array) i start "JavaScript Scratchpad" Firefox with Shift + f4 and then it runs the script there, is just be on the page i want. If i want to do something similar in php, how can i run this script on my browser or is it done another way?
Thanks!
I just want to ask the logic of making a chatroom. Is it correct if i prepare a table in MYSQL, php script to submit whatever words typed and use AJAX to refresh every second to be sure that all input displayed thanks probably a very stupid question, but i pose it because it's counterintuitive for me to write it: i find in alot of wordpress code if statements like: if ( false === $var) i suppose it's the same as if ( $var === false) ? why would one right it in the first way? Hi, I just started learning PHP... Below is my first programme infact.. Out put is not coming when i type some thing in input boxes... Can somebody suggest where is the error... Index.php <html> <head> <title>Welcome to our site</title> </head> <body> <form method="POST" action="submit.php"> <label for ="business_name">Business name</label> <input type="text" id="business_name" \><br/> <label for ="contact_person">Contact person</label> <input type="text" id="contact_person" \><br/> <label for ="Telephone">Telephone</label> <input type="text" id="Telephone" \><br/> <label for ="FAX">FAX</label> <input type="text" id="FAX" \><br/> <label for ="Mobile">Mobile</label> <input type="text" id="Mobile" \><br/> <label for ="Addres_line_1">Addres_line_1</label> <input type="text" id="Addres_line_1" \><br/> <label for ="Addres_line_2">Addres_line_2</label> <input type="text" id="Addres_line_2" \><br/> <label for ="Addres_line_3">Addres_line_3</label> <input type="text" id="Addres_line_3" \><br/> <label for ="Addres_line_4">Addres_line_4</label> <input type="text" id="Addres_line_4" \><br/> <label for ="City">City</label> <input type="text" id="City" \><br/> <label for ="Pin_code">Pin_code</label> <input type="text" id="Pin_code" \><br/> <label for ="State">State</label> <input type="text" id="State" \><br/> <label for ="email">email</label> <input type="text" id="email" \><br/> <input type="submit" value="Please submit the details" name="submit"/> </form> </body> </html> submit.php <head><title>Your submission details</title></head> <body> <H3>YOU HAVE SUBMITTED THE FOLLOWING DETAILS</H3> <?php $business_name= $_POST["business_name"]; $contact_person = $_POST["contact_person"]; $Telephone = $_POST["Telephone"]; $fax = $_POST["FAX"]; $mobile = $_POST["Mobile"]; $address1 = $_POST["Addres_line_1"]; $address2 = $_POST["Addres_line_2"]; $address3 = $_POST["Addres_line_3"]; $address4 = $_POST["Addres_line_4"]; $city = $_POST["City"]; $pincode = $_POST["Pin_code"]; $state = $_POST["State"]; $email = $_POST["email"]; echo "Your business name".$business_name.'<br/>'; echo "Contact person".$contact_person.'<br/>'; echo "Telephone no.".$Telephone.'<br/>'; echo "Fax No.".$fax.'<br/>'; echo "Mobile No.".$mobile.'<br/>'; echo "Address line one".$address1.'<br/>'; echo "Address line two".$address2.'<br/>'; echo "Address line three".$address3.'<br/>'; echo "Address line four".$address4.'<br/>'; echo "Your City".$city.'<br/>'; echo "Your Pincode".$pincode.'<br/>'; echo "Your State".$state.'<br/>'; echo "Your email".$email.'<br/>'; ?> <a href=index.php>Home</a> </body> </html> I have inherited some pages from someone else who wrote them and as I am a newbie and the code is quite old I suspect it needs to be updated in order to work. I have two variables $p and $service that were passed from page 1 to page 2 via the URL. On page 2 I have updated the code for echo $_GET['$p'] etc and I am happy with that. Lower down on page 2 within an HTML form the author has Code: [Select] <input type="hidden" name="service" value="<? echo $service; ?>" /> <input type="hidden" name="p" value="<? echo $p; ?>" /> with the intention of putting these variables in the URL for page 3 I have to assume. Page 3 begins with this code Code: [Select] "completeyourorder.php?p=" . $p . "&service=" . $service; but the variable values are not showing up in the URL and I can not seem to use them on page 3. In other words they don't seem to be available to me. I'd appreciate help to change the page 3 code above or the page 2 code so that it works as intended. I found some posts and researched tutorials but all that I have tried has not worked. Thanks Glenn sorry guys its been a long day and this is an extreme newbie question but for whatever reason right now I cant wrap my head around the concept right now... say for instance one wanted to make a db for job postings, where one could go and see many different categories etc etc. Would you create a table "jobs" and then create rows for the types ie) Accounting, Business, Customer Service, etc. - or would those be all seperate tables as well? Cause im just thinking of the INSERT INTO query, and say I wanted to add a job but I only wanted to add one to the Customer service portion of the jobs table...thats why im curious if everything should be different tables so you could just do INSERT INTO customer_service etc etc and then have all of the persons names/creditials there? OR is it possible to create a table within a table? or am I simply going nuts here? lol. thanks OK, I think this is easy, but I just can't seam to get it figured out. I have this piece of code echo '<img src="maps/' . $map . '.jpg" />'; And what I want to do is if that $map is not in the directory I want it to display a standard image that I know is in the directory. So basically I want the alt tag to be a image instead of text. <?php if (!isset($_GET['register'])) { echo "register stuff here"; } elseif (!isset($_GET['login'])) { echo "login stuff here"; } elseif (!isset($_GET['forgotpassword'])) { echo "password recovery stuff here"; } else { echo "default page stuff here"; } ?> you get what i'm going for, but it kinda messes up this way with different contents on different places? help I have several fields in a database in which some could be blank. I'd like to echo out 'Not Assigned' if that's the case. Based on my limited experience with PHP this is the way I'm going about it: Code: [Select] if ($tech == "") { $tech = "Not Assigned"; } if ($crew == "") { $crew = "Not Assigned"; } if ($number == "") { $number = "Not Assigned"; } I'm pretty certain there is a way to simply place all of the variables into an array to determine if they are empty or not - Followed by changing the variable to 'Not Assigned' one time rather then repeating code over and over again. The way I'm doing it will work, however, something tells me there is a much more efficient way. I can't figure out how I'm messing up so badly. Anyway, I have:
$con=mysqli_connect("removedforsecurity"); $finderCleanup = mysqli_query($con, "SELECT * FROM Entries ORDER BY PID DESC LIMIT 1"); $finderCleanupArray = mysqli_fetch_array($finderCleanup); $integerCleanup = $finderCleanupArray['PID'] - 40; $lastPID = $finderCleanupArray['PID']; $lastPIDreduced = $finderCleanupArray['PID'] - 8;When I var_dump $lastPID, it returns INT with a number (it works correctly.) Then, I have this: function getResults($con,$lastPIDreduced)$con works correctly in the function, but $lastPIDreduced is "undefined." What is my sin? Thanks. Okay, So I'm trying to write a query that will update a certain value in my database, but I want it to update only when 3 conditions are met. Basically, I want to update a column of ints in my DB (mysql) from 0 to a 1, if these criteria are met. 0 and 1 are the only values that are valid in this particular column. dbquery("UPDATE times SET approval = '1' WHERE id = '$id' AND time <= '$currentTime' AND project = '$project'"); that is my query. Now, I'm not even sure if THIS part works. It is however, slightly more complicated in that this query is actually embedded in a foreach loop: $result = $_REQUEST["projects"]; $delimiter - ","; $projectArray = explode($delimiter, $result); foreach($projectArray as $project){ dbquery("UPDATE times SET approval = '1' WHERE id = '$id' AND time <= '$currentTime' AND project = '$project'"); } If you can make any sense of that, and if you can point out any glaringly obvious mistakes here, I would appreciate it. If you need any other information let me know. Thank you all for your help. |