I just realized that you can't just use an if statement on a function, for example this doesn't work:
function sayHello()
{
echo "Hello World";
}
if(sayHello())
echo "Function Worked";
else
echo "Function Failed";
I also saw that a function can't be put as the value of a variable. So how can I do an if statement to check if a function has executed properly and display it to the browser?
It's not working since sayHello() doesn't return anything place return true in there or something.
if (sayHello() === FALSE)
echo "Function Failed";
else
echo "Function Worked";
this will work
<?php
$name1 = "bobi";
function hello($name) {
if($name == "bobi"){
echo "Hello Bob";
} else {
echo "Morning";
}
}
hello($name1);
?>
function selam($isim)
{
if ($isim == 'ugur') {
return 'Selam '.$isim.' :)';
}
return 'Sen kimsin kardes?';
}
echo selam('ugur');
Related
I need your help to pass a bool value of the variable in the view from the controller & get the output in view if $a ==true then Output:1. And if $a==false, than output:2.
For Eg:
My view is like:
if(!$a) {
echo "Hi";
} else{
echo "Hello World";
}
How can I pass the $a bool value from controller to view ??
If $a is a checkbox then make value of checkbox as 1 then
if ( $a==1 ){
echo "Checked";
} else {
echo "not checked";
}
In Controller Code:-
if($statement == true) {
$data['output']="something will be generate here";
}
else if ($statement == false) {
$data['output']="something else be generate here";
}
else {
$data['output']="nothing";
}
$this->load->view('page', $data);
In View:
<?php
echo $output;
?>
Following is my code here actually o/p should be hi..but it is giving no
<?php
$arr=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$c='xyz,ccc';
if(in_array(isset($c) && $c,$arr))
{
echo 'hi';
}
else
{
echo 'no';
}
?>
output:hi
actual result should be 'no'.
Side note, this is bad code:
in_array(isset($weekendArr) && $weekendArr,$arr)
do it like
isset($weekendArr) && in_array($weekendArr,$arr)
and in_array is not strict so this
in_array(true,array('w','s'))
will be allways TRUE
do it with:
in_array(true,array('w','s'),true)
and you see.
And you can't check an array with an array the $needle be an STRING here.
The only solution is to do splitt your STRING into two values and then check two times for TRUE
$c='Sunday,Monday';
foreach(explode(',',$c) as $check){
if(in_array($c,$arr,true))
{
echo $check.' is in array';
}
else
{
echo $check.' is NOT in array';
}
}
Hope that helps a little.
<?php
$listDays=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$day='Sunday'; //You cant test both days ! Just one value at a time
if(true === in_array($day, $listDays))
{
echo 'hi';
}
else
{
echo 'no';
}
?>
Or option two if you want to test different days
<?php
$listDays=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$dayToTest='Sunday, Monday'; //Here we have multiple days
$tabTest = preg_split(',', $day); //split into an array
//Then test for each string in tabTest
foreach($tabTest as $string)
{
if(true === in_array($string, $listDays))
{
echo $string.' is OK';
}
else
{
echo 'no';
}
}
?>
Change your code to:
<?php
$arr=array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$c='Sunday,Monday';
if(in_array(isset($c) && $c,$arr))
{
echo 'hi';
}
else
{
echo 'no';
}
So I've been trying to devise a function that will echo a session variable only if it is set, so that it wont create the 'Notice' about an undefined variable. I am aware that one could use:
if(isset($_SESSION['i'])){ echo $_SESSION['i'];}
But it starts to get a bit messy when there are loads (As you may have guessed, it's for bringing data back into a form ... For whatever reason). Some of my values are also only required to be echoed back if it equals something, echo something else which makes it even more messy:
if(isset($_SESSION['i'])){if($_SESSION['i']=='value'){ echo 'Something';}}
So to try and be lazy, and tidy things up, I have tried making these functions:
function ifsetecho($variable) {
if(!empty($variable)) {
echo $variable;
}
}
function ifseteqecho($variable,$eq,$output) {
if(isset($variable)) {
if($variable==$eq) {
echo $output;
}
}
}
Which wont work, because for it to go through the function, the variable has to be declared ...
Has anyone found a way to make something similar to this work?
maybe you can achieve this with a foreach?
foreach ($_SESSION as $variable)
{function ifseteqecho($variable,$eq,$output) {
if($variable==$eq) {
echo $output;
}
else echo $variable;
}
}
now this will all check for the same $eq, but with an array of corresponding $eq to $variables:
$equiv = array
('1'=>'foo',
'blue'=>'bar',);
you can check them all:
foreach ($_SESSION as $variable)
{function ifseteqecho($variable,$equiv) {
if(isset($equiv[$variable])) {
echo $equiv[$variable];
}
else {
echo $variable;
}
}
}
Something like this?, you could extend it to fit your precise needs...
function echoIfSet($varName, array $fromArray=null){
if(isset($fromArray)){
if(isset($fromArray[$varName])&&!empty($fromArray[$varName])){
echo $fromArray[$varName];
}
}elseif(isset($$varName)&&!empty($$varName)){
echo $$varName;
}
}
You may use variable variables:
$cat = "beautiful";
$dog = "lovely";
function ifsetecho($variable) {
global $$variable;
if(!empty($$variable)){
echo $$variable;
}
}
ifsetecho("cat");
echo "<br/>";
ifsetecho("dog");
echo "<br/>";
ifsetecho("elephant");
UPDATE: With a rather complex code I’ve managed to meet your requirements:
session_start();
$cat = "beautiful";
$dog = "lovely";
$_SESSION['person']['fname'] = "Irene";
function ifsetecho($variable){
$pattern = "/([_a-zA-Z][_a-zA-Z0-9]+)".str_repeat("(?:\\['([_a-zA-Z0-9]+)'\\])?", 6)."/";
if(preg_match($pattern, $variable, $matches)){
global ${$matches[1]};
if(empty(${$matches[1]})){
return false;
}
$plush = ${$matches[1]};
for($i = 2; $i < sizeof($matches); $i++){
if(empty($plush[$matches[$i]])){
return false;
}
$plush = $plush[$matches[$i]];
}
echo $plush;
return true;
}
return false;
}
ifsetecho("cat");
echo "<br/>";
ifsetecho("dog");
echo "<br/>";
ifsetecho("elephant");
echo "<br/>";
ifsetecho("_SESSION['person']['fname']");
echo "<br/>";
ifsetecho("_SESSION['person']['uname']");
echo "<br/>";
What should be passed into the if() to print the output as "Hello World"? [Note: It should execute the else block.]
if(?){
} else {
echo "World";
}
I needs to evaluate to false, and print "Hello" at the same time. printf returns the length of outputted string upon success which is evaluated to true when read in a Boolean context. So reversing that will evaluate to false, executing the else block.
if(!printf("Hello ")){
} else {
echo "World";
}
!printf("Hello ")
By default, printf in 'C' returns true.
if(!printf("Hello "))
{}
else
{
echo "World";
}
You can do in this way...
There is also an alternate solution for this question:
class test{
function __construct()
{
echo "Hello";
}
}
if(!new test){
}else{
echo "World";
}
Anything that evaluates to FALSE.
if understands logical result, i mean TRUE-FALSE
so any any condition that results in true/false results matters for if so you can use
if(true){
echo 'this is executed';
}else{
echo "world";
}
OR
if(false){
echo 'this is executed';
}else{
echo "world";
}
i hope this works
if(printf("Hello ")) {
}
else{
echo "World";}
i think this is enough.....sorry if not
How can I create a function in PHP that checks if a file input is selected? The following is what I have but its not working.
Thank you!
function mcFileUp($mcFile){
if(empty($_FILES[$mcFile]['name'])){
echo 'empty';
}
else{
echo 'Ok!';
}
}
I figured it out:
function mcFileUp($mcFile){
if(empty($mcFile))
{
echo 'empty';
}
else
{
echo 'Ok!';
}
}
mcFileUp($_FILES['mcFileUpload']['name']);
Here is a possible solution
function mcFileUp($mcFile)
{
if(strlen($_FILES[$mcFile]['name'])==0)
{
echo 'empty';
}
else
{
echo 'Ok!';
}
}
if(isset($_FILES["uploaded_file"]))
{
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0))
{
............
}
else
{
echo "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
}
}
else
{
echo "Error: No file uploaded";
}
Check THIS page if you want to know props and corns about the superglobal array "$_FILE". Also, you can have a look # THIS tutorial. Hope it helps you...