php - echo not showing without other echo - php

I got this error and I don't really know why.
switch($op)
{
case "check" :
$result = $Login->login($data,array());
break;
}
echo json_encode($result);
and, it's not echoing the string.
if I put an echo before or after, it works
echo json_encode($result);
echo '.';
returns
{"status":false,"errID":4,"errTxt":"Invalid password"}.
var_dump returns
string(50ish) "{"status":false,"errID":4,"errTxt":"Invalid password"}"
Any ideeas why it's not echoing it without other stuff echoed too?
Edit
$str = json_encode($result);
echo $str;
returns nothing but
$str = json_encode($result);
$str .= '-';
echo $str;
returns
{"status":false,"errID":3,"errTxt":"Invalid email"}-
So it won't echo the string, but if I append something after it, it will echo it.
LE: with die() after echo sill nothing

Related

How to check if a string contains an exact substring in Php

I am trying to check if a number say 1 is present in a string say 11,12,13 To do this I am using strpos:
<?
$s = "2,3,11,12,13";
$ss = "1";
if(strpos($s, $ss)) echo "success";
else echo "fail";
?>
Here I am expecting the code to give fail as output but it gives success.
Is there any function I can use to exactly match the number in a string. I want the number 1 in the string, not the numbers containing 1 like 11,123,100 etc.
Edit 1: It works fine for #Twinfriends answer but the problem is doing it with multiple numbers.
<?
$s = "2,3,11,12,13";
$ss = "1,2";
if(strpos($s, $ss)) echo "success";
else echo "fail";
?>
This code gives output fail but it should give true.
Edit 2: Check answer https://stackoverflow.com/a/48297002/8490014
The problem has been solved but because of for each loop the output is getting repeated as many times as there are values in the array. How can we get the output repeating the output?
You could do it like this:
<?php
$s = "2,3,11,12,13";
$ss = "1";
$toTest = explode(",", $s);
if(in_array("$ss", $toTest)) {
echo "success";
}
else {
echo "fail";
}
?>
If you've any question considering this code, feel free to ask. (Code tested, works well)
The problem has been solved by adding a foreach loop:
<?
$s = "2,3,11,12,13";
$ss = "1,2,3,5";
$catid = explode(",", $s);
$ocat = explode(",",$ss);
foreach($ocat as $abc) {
if(in_array($abc, $catid)) {
echo "success";
}
else {
echo "fail";
}
}
?>
Try this:
<?php
$s = "2,3,11,12,13";
$ss = "1";
$haystack = explode(",",$s);
$matches = preg_grep ("/$ss/", $haystack);
echo !empty($matches) ? "success" : "fail";
You can check by appending comma after the variable to check occurrence at start, appending comma before the variable to check occurrence at last or appending comma before and after the variable to check occurrence at middle.
if((substr($s, 0, (strlen($ss)+1)) === $ss.',')||(substr($s, (strlen($s)-strlen($ss)-1), strlen($s)) === ','.$ss)||(strpos($s, ','.$ss.','))){
echo "success";
}
else{
echo "fail";
}

How to JSON decode with \\n in php

I have a JSON structure as below
$json = '{"Number1":"{\"answerPhrase\":\"\\nTEXT,\\nTEXT
TEXT\\n\",\"dateUpdatedInMillisecond\":1234}"}';
When trying to extract the text and numbers I can do the first step and it works but the nested JSON has \\n and it does not give the text as output
The PHP code is as below
$result = json_decode($json);
print_r($result);
echo "<br>";
foreach($result as $key=>$value){
echo $key.$value;
echo "<br>";
$result_nest = json_decode($value);
echo $result_nest->answerPhrase;
echo "<br>";
Why can I not get the text in answerphrase? It works when the text does not have \\n
You may try the below one. You can replace the \n with some other characters. If you want to display enter in browser then you can replace \n with . Please try the below code and let me know if it worked for you.
<?php
$json = '{"Number1":"{\"answerPhrase\":\"\\nTEXT,\\nTEXT TEXT\\n\",\"dateUpdatedInMillisecond\":1234}"}';
$result = json_decode($json);
print_r($result);
echo "\n";
foreach($result as $key=>$value){
echo $key.$value;
echo "<br>";
$value = preg_replace("/\\n/", "___n___", $value);
$result_nest = json_decode($value);
$result_nest->answerPhrase = preg_replace("/___n___/", "\n", $result_nest->answerPhrase);
echo $result_nest->answerPhrase;
echo "<br>";
}
Prefer to fix json before decode and then decode the sub json.
you could do second step in a loop
function test2()
{
$string = '{"Number1":"{\"answerPhrase\":\"\\nTEXT,\\nTEXT
TEXT\\n\",\"dateUpdatedInMillisecond\":1234}"}';
$json = $this->decodeComplexJson($string);
$number = $json->Number1;
//Put this in a loop if you want
$decodedNumber = $this->decodeComplexJson($number);
var_dump($decodedNumber);
echo $decodedNumber->answerPhrase;
}
function decodeComplexJson($string) { # list from www.json.org: (\b backspace, \f formfeed)
$string = preg_replace("/[\r\n]+/", " ", $string);
$json = utf8_encode($string);
$json = json_decode($json);
return $json;
}

Whats wrong with my link?

I am trying to echo a array value in a link, but it is coming up in dreamweaver as an error, but I cant work out what I have done wrong, can anyone tell me what is wrong with this line please ?.
thanks :-)
echo '';
EDIT >>>>>>>>>>>>>>>>>>>>>>>>
THIS IS THE FULL CODE :
$result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addemailtemplate");
while ($row = mysql_fetch_object($result)) {
echo '<div class="namerow">';
echo '<th>';
echo $row->emailformname;
echo '</th>';
echo '</div>';
echo '<div class="messagerow">';
echo '<th>';
echo $row->emailformmessage;
echo 'dssd';
echo '<tr></tr>';
echo '</div>';
}
echo '</th>';
mysql_free_result($result);
If I echo the cf_uid
echo $row->cf_uid;
this works fine and displays the unique id for each record next to it in the table, I just need to take that id thats is being echo'd and put in at the end of the link so that it looks like http://link&token=2626382837728 << (cf_uid)
FIXED !
Thanks for everyone's help on this work, I worked out what was wrong in the end, what I thought was an array didn't appear to be, this code worked in the end >>
$result = mysql_query("SELECT * FROM hqfjt_chronoforms_data_addemailtemplate");
while ($row = mysql_fetch_object($result)) {
echo '<div class="namerow">';
echo '<th>';
echo $row->emailformname;
echo '</th>';
echo '</div>';
echo '<div class="messagerow">';
echo '<th>';
echo $row->emailformmessage;
$id = $row->cf_uid;
echo 'LINK';
echo '<tr></tr>';
echo '</div>';
}
echo '</th>';
mysql_free_result($result);
Try to separate the strings and the variables.
echo '';
update: If you get an error in this line the problem could be the line before!
alternatively you can try this
echo '';
This way you can just concat the string
echo "SOME NAME FOR THE LINK";
[edit based on updated post]
Use this:
echo 'dssd';
However, I must ask... is there an array variable $detail which has a key 'cf_uid'? And what syntax error are you getting (after you tried this)?
[edit based on comment]
Since it's $row and since it's an object:
echo 'dssd';

Echo / Print works for an object string child, but not elsewhere

$s = $testObj->str;
// hypothetical
echo $s
// works
however
$out = "foo" . $s . "bar;
echo $out;
doesn't
tried () typecasting and strval, can't really think of anything else to do
You have a typo in 2nd string. Should be:
$out = "foo" . $s . "bar";
echo $out;
actually you can do
$str = "foo{$testObj->str}bar";
echo $str;
here .. a testcase : http://codepad.viper-7.com/8PCCkK

$string is not showing up in php search

Just got some help with my ajax/php search but now my issue is that the link is not even showing up on the search page. The echo results are showing up but the $string isn't.
Thanks for the help.
//echo $query;
$result = mysqli_query($link, $query);
$string = '';
if($result){
if(mysqli_affected_rows($link)!=0){
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
echo '<p> <b>'.$row['title'].'</b> '.$row['post_ID'].'</p>' ;
$string .= "<p><a href='set-detail.php?recordID=".$row['post_ID']."'>".$row['title']."</a></p>";
}
} else {
echo 'No Results for :"'.$_GET['keyword'].'"';
}
I don't see where you echo $string. If you put the echo command in there for $string it may begin to work.
You're not printing $string anywhere.
Are you sure you meant to use $string, and not just echo it, like you have with the line above?
You need to include echo $string; or simple echo the line as it is generated if you are calling this inline i.e.
echo "<p><a href='set-detail.php?recordID=".$row['post_ID']."'>".$row['title']."</a></p>";
You do not echo the string above.
After your loop you need to echo it.
if(mysqli_affected_rows($link)!=0){
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)){
echo '<p> <b>'.$row['title'].'</b> '.$row['post_ID'].'</p>' ;
$string .= "<p><a href='set-detail.php?recordID=".$row['post_ID']."'>".$row['title']."</a></p>";
}
echo $string;

Categories