if-statement

How to reduce the number of if-else statements in PHP?

大憨熊 提交于 2020-01-01 03:09:12
问题 I found that there are many if-else statements, especially nested if else statements, these statements make my code less readable. How to reduce the number of if else statements in PHP? My tips are as follows: 1.Use a switch statement when it is suitable; 2.use exit() statement when it is feasible; 3. Use ternary statement when it is feasible; Are there other tips that can reduce if else statements, especially nested if-else statements? 回答1: Refactor your code into smaller work units. Too

How to combine two IF statements in PHP

自作多情 提交于 2020-01-01 02:46:07
问题 Okay i know this is a newb question, but how would i go about only performing IF 2 if IF 1 (text: test appears in data string.) I've tried combining the two but end up with all sorts of issues. So if test doesnt show up the loops skipped, if it does then the regex code i have in IF 2 will be ran. $data = 'hello world "this is a test" last test'; // IF 1 if (stripos($data, 'test') !== false) { } // IF 2 if (preg_match('/"[^"]*"/i', $data, $regs)) { $quote = str_word_count($regs[0], 1); $data =

Python inline elif possible?

随声附和 提交于 2020-01-01 01:56:07
问题 'Hello ' + ('there' if name is None else name) Is the equivalent of msg = 'Hello ' if name is None: msg += 'there' else: msg += name What is the equivalent of this: msg = 'Hello ' if name is None: msg += 'there' elif name == 'Mr Anderson' msg += 'Neo' else: msg += name EDIT: for reference here is the code I wanted to shrink srepr = '\'Modify ' if self.register == 'p': srepr += 'Pointer' elif self.register == 'v': srepr += 'Value' else srepr += 'Unknown' srepr += ' By ' + str(self.delta) + '\'

Only allow letters and numbers for a UITextField

半腔热情 提交于 2020-01-01 00:49:15
问题 I have a signup view controller where the user needs to enter an email address, a username, and a password. For the username and password fields, right now you can currently sign up with a username like "user%$&" and a password like "password%^&$". I don't want people to be able to use these special characters. I want to make it so that the username and password fields can only be submitted with alphanumeric characters aka letters and numbers. I have found some methods to do this, but they

Javascript - check if div contains a word?

北慕城南 提交于 2019-12-31 11:36:12
问题 How can I check if a div contains a certain word? var divs= document.getElementsByTagName('div'); for (var i = 0, len = divs.length; i < len; ++i) { if (divs[i].text = '*word*'){ //do somthing } } doesn't work. 回答1: use the indexOf function if(divs[i].innerHTML.indexOf("word") !== -1) { // something } 回答2: Use includes() : node.textContent.includes('Some text'); 回答3: if (document.getElementById('divId').innerHTML.indexOf("word") != -1) { } 回答4: Try the String.indexOf() function: if (divs[i]

If conditions in a Makefile, inside a target

拥有回忆 提交于 2019-12-31 10:58:12
问题 I'm trying to setup a Makefile that will search and copy some files (if-else condition) and I can't figure out what exactly is wrong with it? (thou I'm pretty sure it's because a combination of spaces/tabs written in the wrong place). Can I get some help with it, please? Here's what I have currently: obj-m = linuxmon.o KDIR = /lib/modules/$(shell uname -r)/build UNAME := $(shell uname -m) all: $(info Checking if custom header is needed) ifeq ($(UNAME), x86_64) $(info Yes) F1_EXISTS=$(shell [

variable not defined in python

穿精又带淫゛_ 提交于 2019-12-31 07:42:34
问题 Hi I'm very new with programming in python. I'm starting my first program but i'm having a little trouble.. is there a better way of doing the snippet of code below? When i run the program I get "yes_no" not defined. def main(): print "\n Welcome to registration. Are you an authorized user?\n" yes_no = raw_input("Yes or No?: ") if yes_no in ("Yes", "yes"): return oldUser() elif yes_no in ("y", "Y"): print ("Please enter YES") return wrong() elif yes_no in ("n", "N"): return wrong() else:

If statement in c++, freaks out [duplicate]

旧城冷巷雨未停 提交于 2019-12-31 07:39:26
问题 This question already has answers here : While statement not working? (2 answers) Closed 5 years ago . when I try to run this game and wants to end it by typing "slut" or "Slut" (Swedish for 'end') it makes a new player call "slut" or "Slut" BUT when I only have ONE statement in the while loops or If statements it's all working. To make it more clear It works when I have (playerName != "Slut") but I want for safty have (playerName != "Slut") || (playerName != "slut") and then it wont work.

One line if statement in Ruby

寵の児 提交于 2019-12-31 06:58:11
问题 I have following piece of code: if day > 31 day -= 31 month = "April" end Can I write it in one line different than: if day > 31 then day -= 31 and month = "April" end ? I've tried it like: if day > 31 {day -= 31; month = "April"} But it doesn't work 回答1: (day -= 31; month = "April") if day > 31 Alternate way (As suggested by @mudasobwa in comments below) : day, month = day - 31, "April" if day > 31 来源: https://stackoverflow.com/questions/27166803/one-line-if-statement-in-ruby

PHP variable from external file?

左心房为你撑大大i 提交于 2019-12-31 05:41:53
问题 *EDIT / FINISHED SOLUTION / WORKING CODE So, this is what a friend of mine helped me come up with. Here is the part I use in my K2 "items.php" file: <div class="fb-comments" data-href="<?php echo JURI::current(); ?>" data-num-posts="8" notify="true" data-width="580"></div> <input id="authname" style="display: none;" type="text" value="<?php echo $this->item->author->name; ?>" /> <input id="authmail" style="display: none;" type="text" value="<?php echo $this->item->author->email; ?>" /> <input