PHP - True And False Or 1 And 0?
Just a silly question. I've been using 1 and 0 to tell if something is true or false, such as if a feature is enabled or not. I see that some use the words true and false. Is there a proper way or is either way correct? Thanks!
Similar TutorialsI am trying to take all the information from $results and input it into a text file, sometimes we get duplicates in $results so I want to check the text file for the entry before writing it in so we don't get duplicates in the text file. However inarray keeps returning false, even when I echo out the two arrays and they match? Code: [Select] $myfile = "complete.txt"; foreach ($results['info'] as $data) { $output=$fields['firstname']."|".$record['lastname']."|".$record['address']."|".$record['number']."\n"; $handle = fopen($myfile, "r"); $contents = fread($handle, filesize($myfile)); fclose($handle); $list = array(); $list = explode("\n",$contents); $list = array_map("trim", $list); $current = $output; echo in_array($current,$list) ? $current.' exists' : $current.' does not exist'; if (in_array($current,$list)) { print "duplicate"; } else { if($file=fopen($myfile, "a")) { //open file for writing fwrite($file, $output); //write to file } } } i keep seeing people use this kind of code but i cant find any document or help web pages on it. im sure there is but i done know what to call it. i would really like to learn more about it for example Code: [Select] $lang = isset($_GET['lang']) ? (int)$_GET['lang'] : 1; or Code: [Select] return !empty($result_array) ? array_shift($result_array) : false; I have been trying to write code that deletes a book from a database if the user provides a BookName, and the corresponding DeletionCode. I then want to provide the user with feedback, telling the user whether they have deleted a book, that is they have provided a deletion code and a book name, or whether they have not deleted a book, because they have entered the wrong bookname or book ID. I thought this code would work, but it seems that the $result variable is always set as TRUE, no matter whether the code has deleted a row or not. What have i done wrong? $result = mysql_query("DELETE FROM books WHERE BookName = '$_POST[BookName]' && DeletionCode = '$_POST[Deletioncode]'"); if(!$result){ die('Invalid query: ' . mysql_error()); } else echo "delete book"; Ok I guess this is more a algorithm problem than a php problem, but still I hope someone can help me. I'm trying to write some code that starting from two arrays containing observed and expected values will calculate true and false positive and negative rates of a certain event. Let's say I have 30 events and 5 of them are really red while 25 are blue. (each event is identified by an unique id i.e. number) An observer is required to evaluate the 30 events and answer if they are red or blue, their answer (only if blue if stored in the observed array - in the form of a position id) So in the end I have one array that has the observed red events i.e. 4 56 78 44 90 and the expected array with the actual red events i.e. 4 78 33 34 In this case my true positive would be 2 (4 and 78 that are both in the observed and the expected), my false positive would be 3 (observed but not expected), my false negative 2 (expected but not observed) and my true negative 23 (not expected and not observed, obtained substracting the previous three from the total number of events) How can I calculate this four values using PHP, I thought it was easier but as I started to program it I realized I didn't know where to go Thanks to all! MySQL cannot store Boolean values and thus I use 0/1. Is there much benefit to cast them as Boolean immediately after querying the database, do whatever PHP processing is required, and then convert them back to 0/1 before writing to the database? One benefit is I can type declare my arguments, but I am debating whether it is worth it. Thanks Hey Everyone, I want to start out by saying thanks in advance for reading this. Since I started working with the XAMPP package this community has been a great resource, though this is my first post. most of my work has been modifying existing stuff in our network, this is my first project from scratch. I am working on an access control project where I am storing site names and RFID tags in a table, and using PHP to check for existing records. Basically, I want to submit 2 credentials from a host (site and RFID), check if they exist together in the table, if so return TRUE or 1, if not return FALSE or 0. I am new to both PHP and MySQL (although I took a course years ago) so I am looking here for help. On this server I have XAMPP installed and everything I need running. I have a table and created a few records for testing. My current trouble is getting PHP to return the proper values for the SQL query. The SQL query works fine, I think - it returns the number of rows that match and that record. I'm not sure this is the result I need to be able to deal with the way I want. Using other resources my current PHP statement looks like this: Code: [Select] if(mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE 'site_name' LIKE 'berland' AND 'card_id' LIKE '290093C84E' LIMIT 0 , 30"))>0){echo '1';} else {echo '0';} So, my table has the entry as noted in this query above. Final one will be dynamic where the host will be sending those values - I will likely need help with that as well, but small steps first.;-) Anyway, I was playing around with the end statement >0{echo '1';} but wasn't able to make it do what I want. Any help is appreciated. Thank you. Aaron hi there, is there a difference between if($var == false) and if(false == $var) thankyou! $looponce = 1; foreach ($this->info as $k => $v) { if ( (($k == 'subject') && ($v['required'])) && (!$this->settings['customSubject'])) { for ($i = 0; $i <= 0; $i++) { $output[] = $this->display_errors('error_system_subject'); } } if ( (($k == 'name') && (!$v['required'])) || ((!array_key_exists("name", $this->info)) && ($looponce == 1)) ) { $output[] = $this->display_errors('error_system_name'); $looponce++; } } That is my loop that i check things in. What i'm more interested in is the name if statement. If currently checks for a name key in an array(below) and makes sure that it is set to required. If it's not set to required, print out a system error and die()'s. I'm looking for ways to remove the need for program errors and just change them to what is needed to run. What i know how to do is, get into the inner array, what i don't know is how to edit the data and put it back exactly as it was given to me. The inner array data can be put back in any order, but the outer array must be in exact order as it was given. above code $this->info = $formdata; $formdata = array( 'name' => array('name'=>"Full Name", 'required'=>false, 'type'=>'text'), # This needs to be required=>true, but i can't trust the user, which is why i have the error. 'telephone' => array('name'=>"Telephone", 'required'=>false, 'type'=>'phone'), ); Any help is greatly appreciated, also am i doing the foreach loop in the code above in an efficient manner or is there another way? Apart from the fact this can cause an infinite loop is it OK to use? $user->reset_token = $user->generate_password(32); // Make sure the token is unique while(TRUE) { $count = ORM::factory('manager')->where('reset_token', '=', $user->reset_token)->count_all(); if($count < 1) break; $user->reset_token = $user->generate_password(32); } Or would you approach the logic for this differently? Thoughts appreciated. Cheers I am wondering if these "true" values are the same to PHP... Code: [Select] <a href="' . BASE_URL . 'members/log_in.php?addComment=true">Log In</a> Code: [Select] if (isset($_GET['addComment']) && ($_GET['addComment']==TRUE)){ Debbie Is there any reason why I would want to use this... Code: [Select] $_SESSION['loggedIn'] = TRUE; versus this... Code: [Select] $_SESSION['loggedIn'] = 1; (The first one is easier to read...) Debbie <?php $id1 = '5' ; $id = array(1,2,3,4,5,6,7); foreach($id as $value){ if($id1==$value){ echo 'One of them matches, ' ; echo 'Do not execute command <BR>' ; } ////// if ends here } ////// loop ends here ?> How to do something if all statements are false ? like when id $id1 = '10' , ? hi i have set 2 varibles, and want to echo them out if they are true, i have this script but doesnt work, please help. <?php $month=$_GET['month']; $year=$_GET['year']; If($year!=0 && $month!=0) { echo $month; echo $year; } ?> cheers matt Hey here is my code im trying to redirect the index.php to index.php?id=1 the way i have done it doesn't look really good is there a better way to do it please? im trying to redirect to id=1 because otherwise i wont be able to echo out anything... id 1 is default category so is there a way to tell it if $id is empty echo from id 1? <?php $id = $_GET['id']; if($id == ''){ echo "<meta http-equiv=Refresh content=0;url=index.php?id=1>"; } $result = mysql_query("SELECT * FROM pages WHERE id='$id' ",$connect); while($row = mysql_fetch_assoc($result)) { echo $row['anything1']; echo $row['anything2']; } ?> Hi, Apologies if this is rather easy but wondered how you would get around the following problem. I have a post being sent on a checked form that has the name set to a value used in my DB, for example: Code: [Select] <input type="checkbox" name="<?PHP echo $rows ["catID"] ?>"> Where name would equal something like 11. I'm trying to right some code that inserts data from the rows that have been checked, bit like the below: Code: [Select] while($rows = $database->fetch_array($result)){ $strSQL='INSERT INTO affiliate_join_cat (ajcCatID, ajcAffID) VALUES ('.$rows["catID"].', '.$_POST["affID"].')'; if ($_POST($rows["catID"])=="on"){ $database->query($strSQL); so my post will look something like 11 = on, dependent on the row checked. But there is something wrong with this part of the code: Code: [Select] $_POST($rows["catID"])=="on" my thoughts are that until catID actually equals 11 this does not evaluate to anything? So have tried using empty and isset to handle it without any success. Not sure what to do next? Thanks in advance <?php if('00' == '000') echo 'weired'; ?> Please consider the code above. Strangely it echoes the word "weired". Also if('00' == '000000000000000') returns true and so forth. What is really going on? Thanks in advance By the way the php version is 5.3.9 on wampserver 2.2 I'm pulling my hair out trying to figure out a simple problem. I've got the following PHP code in an HTML page: $shutoff_date = strtotime($wkend_start_date) - 259200; if ($shutoff_date > $todays_date) { $current_month = date("m"); $current_year = date("y"); $freshman_eligible = 0; if ($current_month >= 2 && $current_month <=7) { $freshman_eligible = 1; } if ($current_month <= 7) { $seniors = $current_year ; } else {$seniors = $current_year + 1; } $juniors = $seniors + 1; $sophomores = $seniors + 2; $freshman = $seniors + 3; echo "HTML Stuff blah, blah, blah"; if ($freshman_eligible = 1) { echo "<option value=\"$freshman\">$freshman</option>";} } The last test to see if $freshman_eligible is equal to 1 is always testing true, and it's actually resetting $freshman_eligible to whatever I put inside the IF statement. No matter what the variable is set to previously, it will reset to whatever I test for in the last section, and hence always tests true. I thought maybe it was an issue with scope, but I'm not sure what the issue would be or how to resolve it. What am I missing??? Hi Not that often I play with these (and normally just a single font I use). However I am trying to generate an image with a ttf bit of text on it. No problem using Tuffy.ttf (the normal one I use), but I want to use some arrow characters which it doesn't support. I have tried using Wingding and Webdings but neither work with GD. Am I missing something (hopefully something obvious)? All the best Keith I have a statement $account = $_SESSION['account_id']; $postedby = $account_id; /*both echo out to the same number*/ if($account == $postedby){ echo "DELETE BUTTON"; } i echo'ed out both $account and $postedby and both return 1 but the if statement fails to read as true. HELP!!! thanks in advance! |